【问题标题】:how to read command output line by line in gcc in windows just as with the standard input?如何在 Windows 中的 gcc 中逐行读取命令输出,就像使用标准输入一样?
【发布时间】:2015-04-01 13:59:51
【问题描述】:

这是我尝试过的:

#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*&amp;, std::__cxx11::string&amp;)'

我在这里找到的第二个例子:https://stackoverflow.com/a/10702464/393087 但似乎 mingw 没有包含 pstream:fatal error: pstream.h: No such file or directory - 编辑:好的,我知道,我错过了这不是 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

【问题讨论】:

  • 你需要&lt;fstream&gt;
  • std::getline 仅从 C++ 流中读取。要从旧的 C 文件流中读取一行,您需要使用 C 函数 fgets
  • 至于您的第二段代码,不要执行while (!ins.eof()),因为这很少会按预期工作,因为eofbit 标志要到之后才会设置您尝试从文件末尾之外读取,导致循环迭代一次到多次。而是使用while (std::getline(...))
  • @0x499602D2:不,添加 &lt;fstream&gt; 标头不会消除错误。

标签: c++ c++11 mingw


【解决方案1】:

你不能这样做:

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;
}

:)

【讨论】:

  • 我投了赞成票,但答案不是我的意思,它是用于从磁盘读取文件,但我需要的是运行命令并输出其结果。
  • 哈哈,你说得对,我看错了 popen 调用...然后你可以使用 Boost 来实现:#include &lt;boost/noncopyable.hpp&gt; #include &lt;boost/iostreams/stream.hpp&gt; #include &lt;boost/iostreams/device/file_descriptor.hpp&gt; ... FILE* pipe = popen("app.exe", "r"); boost::iostreams::file_descriptor_source source(fileno(pipe), boost::iostreams::never_close_handle); boost::iostreams::stream&lt;boost::iostreams::file_descriptor_source&gt; stream(source, 0x1000, 0x1000); string filename; while (getline(stream, filename)) { ifstream file_stream(filename.c_str(), ifstream::in); .... }
猜你喜欢
  • 2011-06-02
  • 1970-01-01
  • 1970-01-01
  • 2014-04-09
  • 2018-05-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多