system() usage: I need to capture both the return code and the output
Posted on 2002-07-16
sounds simple... I hope so :-)
the system() command returns an int status code when it executes any command.. I need that so I can tell my calling function if the system() command was successful. But the commands that will be running will also have stdout and/or stderr output that I need to capture and return to my caller.
Using system(), an answer would be to redirect output to a standard file by adding " >fileX 2>fileY " to the command, then reading those files afterwards. But this worries me because I don't know what commands might be requested, and somebody might just try to redirect output themselves...
I also considered popen().. where I can easily capture the stdout. But popen doesn't tell me if the command failed, nor does it provide a function for stderr.
Can anyone recommend a solution?