I am trying to implement a function in C++ that runs a shell command and returns the exit code, stdout
and stderr.
I am using the Boost process library
std::vector<std::string> read_outline(std::string & file)
{
bp::ipstream is; //reading pipe-stream
bp::child c(bp::search_path("nm"), file, bp::std_out > is);
std::vector<std::string> data;
std::string line;
while (c.running() && std::getline(is, line) && !line.empty())
data.push_back(line);
c.wait();
return data;
}
In the above example from boost's website, in the while loop the condition c.running() is checked. What if the process finishes executing before the while loop is reached? In that case I won't be able to store the child process's stdout to data. Boost's documentation also mentions the following
[Warning] Warning The pipe will cause a deadlock if you try to read after nm exited
Hence it seems that the check for c.running() should be there in the while loop.
How do I get the stdout (and stderr) from the processes that finish running before the program reaches the while loop?