Re: [問題] C 的 popen() 在 C++ 裡
※ 引述《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 (uranus@tinlans.org)
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)
推
03/26 14:12, , 1F
03/26 14:12, 1F
討論串 (同標題文章)
本文引述了以下文章的的內容:
完整討論串 (本文為第 2 之 2 篇):
C_and_CPP 近期熱門文章
PTT數位生活區 即時熱門文章