Using system() or exec(), I can get any command to execute but they display the result into the Console. I want to execute the command and extract the output and process it, and then display it. How can I achieve this on Windows/DOS Platform?
Using system() or exec() , I can get any command to execute but they
Share
There’s nothing like that in standard C, but usually, for compatibility with POSIX, compilers implement
popen(_popenin VC++), which starts a command (as it would do withsystem) and returns aFILE *to the stream you asked for (you can ask for stdout if you want to read the output or stdin if you want to give some input to the program).Once you have the
FILE *, you can read it with the usualfread/fscanf/…, like you would do on a regular file.If you want to have both input and output redirection things start to get a bit more complicated, since Windows compilers usually do have something like POSIX
pipe, but it isn’t perfectly compatible (mostly because the Windows process creation model is different).In this case (and in any case where you need more control on the started process than the plain
popengives you) I would simply go with the “real” way to perform IO redirection on Windows, i.e. withCreateProcessand the appropriate options; see e.g. here.