作者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