I was wondering if there is a standard-compliant way to execute a command line program/shell script from c++ and get its output, using only facilities in c++11/c++1y. That is, I wanted to avoid POSIX unistd.h
or Windows process.h
.
The closest thing I found was system
in cstdlib
http://www.cplusplus.com/reference/cstdlib/system/, but its return value does not include the output of a program (i.e. stdout
& stderr
). I have also tested pstreams http://pstreams.sourceforge.net/. But upon including its header file, pstream.h
, I got:
pstream.h:44:41: fatal error: sys/wait.h: No such file or directory
Apparently, sys/wait.h is POSIX and not present in MinGW g++, which I use most of the time (4.8.1 as of Mar 2014).
Thanks,
--- EDIT ---
My question boils down to this:
In current c/c++ standard, we conceptually have cout
, cin
etc for programs on the one hand, and system()
on the other to run a program. Is there a way to extract or implement a function to extract the returned output from the program in a standard compliant manner?