Search code examples
c++popen

C++ system() function — How to collect the output of the issued command?


I'm running some commands with the C++ system() function:

int system ( const char * command );

How can I collect the standard output from the issued commands?

To be specific, I want to collect the output of the issued command (for example, the directory listing output from issuing the dir command).


Solution

  • Are you looking for returned value (as in "exit status") of the executed command, or for its output (as in "what did it print")?

    If the latter, use popen() and pclose() instead.

    If the former, look at the return value from system() (and use the documentation for waitpid() to interpret it).