Reading popen results in C++

39,063

Your example:

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

Does't work because although you're very close the standard library doesn't provide an fstream that can be constructed from a FILE*. Boost iostreams does however provide an iostream that can be constructed from a file descriptor and you can get one from a FILE* by calling fileno.

E.g.:

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))

Don't forget to pclose later still.

Note: Newer versions of boost have deprecated the constructor which takes just a fd. Instead you need to pass one of boost::iostreams::never_close_handle or boost::iostreams::close_handle as a mandatory second argument to the constructor.

Share:
39,063
Stefano
Author by

Stefano

C++ Software engineer working in control systems for particle accelerators.

Updated on April 04, 2020

Comments

  • Stefano
    Stefano about 4 years

    I am writing a C++ application and I need to read the result of a system command.

    I am using popen() more or less as shown here:

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

    I've been trying to re-write this in a different way. I saw some non-standard solutions like:

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

    My compiler does not accept this though.

    How can I read from popen in C++?

  • Sjoerd
    Sjoerd over 12 years
    Some compilers provide non-standard extensions to the standard C++ library. A fstream constructor that takes a FILE* is a popular one. Which explains why it works on some compilers and not on others.
  • Flexo
    Flexo over 12 years
    @Sjoerd - ah yes that would make sense. I wondered why it would be written like that. Still you can use a typedef to pick between an non-standard extension and a boost library at configure time in your build tool.
  • Stefano
    Stefano over 12 years
    i tried with the standard constructor for fstream but was not accepted in my case. i'll try now in this way...
  • Terry Shi
    Terry Shi over 7 years
    Didn't compile, I guess the sink should be source, since it's a read only example. boost::iostreams::file_descriptor_source