作者tinlans ( )
看板C_and_CPP
标题Re: [问题] C 的 popen() 在 C++ 里
时间Thu Mar 26 14:05:04 2009
※ 引述《dbzgtgwduo (迪欧麦斯威尔)》之铭言:
: 我想请问一下,C++ 有没有像 C 的 popen 这种函数
: 我想要使用 C++ 开启一个执行档,然後用 C++ 对他下指令
: 就像用 C++ 开 cmd,下指令 dir,然後把结果传回 C++ 接
: 请问一下有没有人知道要用什麽函数,谢谢。
C++ 还是用 popen (不过这是有乖乖照 POSIX 规范做的 OS 才有的东西),
你如果想用很 C++ 的东西就要靠 Boost.IOStreams 帮你转:
#include <boost/iostreams/device/file_descriptor.hpp>
#include <boost/iostreams/stream.hpp>
...
using namespace boost;
using namespace boost::iostreams;
...
FILE *fp = popen("<command>", "r");
file_descriptor fd(fileno(fp));
stream<file_descriptor> ifstm(fd);
string line;
while(getline(ifstm, line)) {
...
}
...
--
Ling-hua Tseng (
[email protected])
Department of Computer Science, National Tsing-Hua University
Interesting: C++, Compiler, PL/PD, OS, VM, Large-scale software design
Researching: Software pipelining for VLIW architectures
Homepage:
https://www.tinlans.org
--
※ 发信站: 批踢踢实业坊(ptt.cc)
◆ From: 118.160.109.83
※ 编辑: tinlans 来自: 118.160.109.83 (03/26 14:05)
1F:推 Ebergies:与其用 POSIX 还不如自己写自己 port... 真囧 03/26 14:12