如何在 windows 中的 gcc 中逐行读取命令输出,就像标准输入一样?
how to read command output line by line in gcc in windows just as with the standard input?
这是我试过的:
#include <iostream>
#include <string>
int main(int argc, char const *argv[]) {
using namespace std;
for (string cin_line; getline(cin, cin_line);) {
cout << cin_line << endl;
}
FILE* pipe = popen("app.exe", "r");
for (string result_line; getline(pipe, result_line);) {
cout << result_line << endl;
}
pclose(pipe);
return 0;
}
编译不通过,结果是:
no matching function for call to 'getline(FILE*&, std::__cxx11::string&)'
我在这里找到的第二个例子:
但似乎 mingw 没有包含 pstream:fatal error: pstream.h: No such file or directory
- 编辑:好的,我知道,我错过了这不是 GCC 库,它的名字和原来一样,但这是单独下载的: http://pstreams.sourceforge.net/
我知道如何使用缓冲区并在单行上获取整个输出(如下所示:)然后通过 \n
分解行并获取我的数组,但这里的重点是我必须在输入一进来就提供输出。
我也试过这里的例子: - 我已经添加了 main
功能:
#include <cstdio>
#include <iostream>
#include <string>
int main(int argc, char const *argv[]) {
using namespace std;
FILE * fp ;
if((fp= popen("/bin/df","r")) == NULL) {
// error processing and exit
}
ifstream ins(fileno(fp)); // ifstream ctor using a file descriptor
string s;
while (! ins.eof()){
getline(ins,s);
// do something
}
return 0;
}
这也不编译:
error: variable 'std::ifstream ins' has initializer but incomplete type
ifstream ins(fileno(fp)); // ifstream ctor using a file descriptor
你不能这样做:
FILE* pipe = popen("app.exe", "r");
for (string result_line; getline(pipe, result_line);) {
cout << result_line << endl;
}
pclose(pipe);
您需要这样做:
#include <boost/noncopyable.hpp>
#include <boost/iostreams/stream.hpp>
#include <boost/iostreams/device/file_descriptor.hpp>
FILE* pipe = popen("app.exe", "r");
boost::iostreams::file_descriptor_source
source(fileno(pipe), boost::iostreams::never_close_handle);
boost::iostreams::stream<boost::iostreams::file_descriptor_source>
stream(source, 0x1000, 0x1000);
string result_line;
while (getline(stream, result_line)) {
cout << result_line << endl;
}
:)
这是我试过的:
#include <iostream>
#include <string>
int main(int argc, char const *argv[]) {
using namespace std;
for (string cin_line; getline(cin, cin_line);) {
cout << cin_line << endl;
}
FILE* pipe = popen("app.exe", "r");
for (string result_line; getline(pipe, result_line);) {
cout << result_line << endl;
}
pclose(pipe);
return 0;
}
编译不通过,结果是:
no matching function for call to 'getline(FILE*&, std::__cxx11::string&)'
我在这里找到的第二个例子:
但似乎 mingw 没有包含 pstream:fatal error: pstream.h: No such file or directory
- 编辑:好的,我知道,我错过了这不是 GCC 库,它的名字和原来一样,但这是单独下载的: http://pstreams.sourceforge.net/
我知道如何使用缓冲区并在单行上获取整个输出(如下所示:)然后通过 \n
分解行并获取我的数组,但这里的重点是我必须在输入一进来就提供输出。
我也试过这里的例子: - 我已经添加了 main
功能:
#include <cstdio>
#include <iostream>
#include <string>
int main(int argc, char const *argv[]) {
using namespace std;
FILE * fp ;
if((fp= popen("/bin/df","r")) == NULL) {
// error processing and exit
}
ifstream ins(fileno(fp)); // ifstream ctor using a file descriptor
string s;
while (! ins.eof()){
getline(ins,s);
// do something
}
return 0;
}
这也不编译:
error: variable 'std::ifstream ins' has initializer but incomplete type
ifstream ins(fileno(fp)); // ifstream ctor using a file descriptor
你不能这样做:
FILE* pipe = popen("app.exe", "r");
for (string result_line; getline(pipe, result_line);) {
cout << result_line << endl;
}
pclose(pipe);
您需要这样做:
#include <boost/noncopyable.hpp>
#include <boost/iostreams/stream.hpp>
#include <boost/iostreams/device/file_descriptor.hpp>
FILE* pipe = popen("app.exe", "r");
boost::iostreams::file_descriptor_source
source(fileno(pipe), boost::iostreams::never_close_handle);
boost::iostreams::stream<boost::iostreams::file_descriptor_source>
stream(source, 0x1000, 0x1000);
string result_line;
while (getline(stream, result_line)) {
cout << result_line << endl;
}
:)