如何在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&)'
我在这里找到的第二个例子:https://stackoverflow.com/a/10702464/393087但mingw似乎没有包含pstream:fatal error: pstream.h: No such file or directory
-edit:好吧,我知道,我错过了这不是GCC库,它的名称和以前一样,但这是单独的下载:http://pstreams.sourceforge.net/
我知道如何使用缓冲区并在单行上获得整个输出(如这里:https://stackoverflow.com/a/478960/393087),然后用n
分解行,得到我的数组,但这里的要点是,我必须在输入一进来就提供输出
此外,我还尝试了以下示例:https://stackoverflow.com/a/313382/393087-我添加了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;
}
:)
相关文章:
- 从标准输入读取文件后读取用户输入
- 如何从标准输入中检测格式化输入错误?
- C++ - 标准输入环路
- 快速读取标准输入缓冲区,无需检查
- 将标准输入中的双精度存储在矢量中
- 如何将 getline 与标准输入一起使用?
- 从 C++ 中的标准输入迭代读取字节
- C++计算标准输入的字数
- 如何从 c++ 中的给定标准输入中获取每个字符的偏移量?
- 为什么此代码从标准输入中少一个输入?
- 如何在C++中从标准输入中获取变量输入
- C++ 从标准输入读取文件名
- 从标准输入读取多个类型时出现问题
- 如何在execl中使用标准输入'<'?
- 如何在 eclipse 中将标准输入从控制台更改为另一个程序
- 流对象来表示文件输入,然后是标准输入?
- 标准输入输出操作如何C++完成
- 从标准输入读取时zmq_poll不侦听信号
- 是否可以从标准输入指向指针的地址
- 如何在windows中的gcc中逐行读取命令输出,就像使用标准输入一样