【问题标题】:Reading popen results in C++在 C++ 中读取 popen 结果
【发布时间】:2011-12-10 02:25:58
【问题描述】:

我正在编写一个 C++ 应用程序,我需要读取系统命令的结果。

我或多或少地使用popen(),如下所示:

    const int MAX_BUFFER = 2048;
    string cmd="ls -l";
    char buffer[MAX_BUFFER];
    FILE *stream = popen(cmd.c_str(), "r");
    if (stream){
       while (!feof(stream))
       {
            if (fgets(buffer, MAX_BUFFER, stream) != NULL)
            {
               //here is all my code
            }
       }
       pclose(stream);
    }

我一直在尝试以不同的方式重新编写它。我看到了一些非标准的解决方案,例如:

FILE *myfile;
std::fstream fileStream(myfile);
std::string mystring;
while(std::getline(myfile,mystring))
{
    // .... Here I do what I need
}

我的编译器不接受这个。

如何在 C++ 中读取 popen

【问题讨论】:

  • 什么是here all my code?如果是data.append(buffer);,您的第一个解决方案就可以完美运行。
  • 能否发布崩溃时的调用堆栈?
  • 请提供一个最小的完整程序来证明您的错误。从您的实际程序开始,删除所有有效的行,然后向我们展示剩下的内容。 Here 是您的第一个代码片段的工作实现示例。有关使用此技术的更多信息,请参阅sscce.org
  • 没有。如果结果为空,它会在 fgets 上崩溃
  • 伙计们...我很抱歉...但是你是对的...错误在其他地方...我处于盲目模式,确定错误在那里但它是不!...感到很抱歉一直坚持..

标签: c++ file stream popen fstream


【解决方案1】:

你的例子:

FILE *myfile;
std::fstream fileStream(myfile);
std::string mystring;
while(std::getline(myfile,mystring))

不起作用,因为尽管您非常接近标准库并没有提供可以从FILE* 构造的fstream。然而Boost iostreams 确实提供了一个iostream,它可以从一个文件描述符中构造出来,您可以通过调用filenoFILE* 中获取一个。

例如:

typedef boost::iostreams::stream<boost::iostreams::file_descriptor_sink>
        boost_stream; 

FILE *myfile; 
// make sure to popen and it succeeds
boost_stream stream(fileno(myfile));
stream.set_auto_close(false); // https://svn.boost.org/trac/boost/ticket/3517
std::string mystring;
while(std::getline(stream,mystring))

以后别忘了pclose

注意:较新版本的 boost 已弃用仅采用 fd 的构造函数。相反,您需要将boost::iostreams::never_close_handleboost::iostreams::close_handle 之一作为强制的第二个参数传递给构造函数。

【讨论】:

  • 一些编译器为标准 C++ 库提供非标准扩展。采用 FILE* 的 fstream 构造函数是一种流行的构造函数。这就解释了为什么它适用于某些编译器而不适用于其他编译器。
  • @Sjoerd - 是的,这是有道理的。我想知道为什么会这样写。您仍然可以在构建工具的配置时使用 typedef 在非标准扩展和 boost 库之间进行选择。
  • 我尝试使用 fstream 的标准构造函数,但在我的情况下不被接受。我现在就这样试试……
  • 没有编译,我猜 sink 应该是 source,因为它是一个只读的例子。 boost::iostreams::file_descriptor_source
【解决方案2】:

这是我很久以前写的东西,可能会对你有所帮助。它可能有一些错误。

#include <vector>
#include <string>
#include <stdio.h>
#include <iostream>

bool my_popen (const std::string& cmd,std::vector<std::string>& out ) {
    bool            ret_boolValue = true;
    FILE*           fp;
    const int       SIZEBUF = 1234;
    char            buf [SIZEBUF];
    out = std::vector<std::string> ();
    if ((fp = popen(cmd.c_str (), "r")) == NULL) {
        return false;
    }
    std::string  cur_string = "";
    while (fgets(buf, sizeof (buf), fp)) {
        cur_string += buf;
    }
    out.push_back (cur_string.substr (0, cur_string.size () - 1));
    pclose(fp);
    return true;
}
int main ( int argc, char **argv) {
        std::vector<std::string> output;
        my_popen("ls -l > /dev/null ", output);
        for ( std::vector<std::string>::iterator itr = output.begin();
                                                 itr != output.end();
                                                 ++itr) {
                std::cout << *itr << std::endl;
        }

}

【讨论】:

    猜你喜欢
    • 2018-07-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-04-18
    • 1970-01-01
    • 2010-09-23
    • 2014-08-30
    • 2017-01-20
    相关资源
    最近更新 更多