在C++中读取popen结果

问题描述 投票:0回答:3

我正在编写一个 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

c++ file stream popen fstream
3个回答
16
投票

你的例子:

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

不起作用,因为尽管您非常接近,但标准库不提供可以从

fstream
构造的
FILE*
。然而,Boost iostreams确实提供了一个可以从文件描述符构造的
iostream
,您可以通过调用
FILE*
fileno
中获取一个。

例如:

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_handle
boost::iostreams::close_handle
之一作为强制第二个参数传递给构造函数。


0
投票

我想做大致相同的事情(我有一个需要 std::istream 的函数,因此无法处理 FILE* 参数)并且没有 boost。以下对我有用:

#include <iostream>
class Wrapper : public std::istream {
        private:
                FILE* file;
                bool last_read_good;
        public:
                Wrapper( FILE* file ) {
                        this->file = file;
                        this->last_read_good = true;
                }
                int get() {
                        char c;
                        if( fread( &c , 1 , 1 , this->file ) < 1 ) {
                                this->last_read_good = false;
                        }
                        return c;
                }
                bool good() {
                        return (this->last_read_good);
                }
};
int main() {
        FILE* pipe = popen( "cat some_file" , "r" );
        Wrapper* w = new Wrapper(pipe);
        do {
                std::cout << ((char)(w->get()));
        } while( w->good() );
        delete w;
        return 0;
}

当然,这是一个最小的工作示例。如果你想做任何复杂的事情,你必须重写更多的 std::istream 类。


-4
投票

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

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

}
© www.soinside.com 2019 - 2024. All rights reserved.