4

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?

user1224976
  • 45
  • 12
  • possible duplicate of [How can I run an external program from C and parse its output?](http://stackoverflow.com/q/43116/), [Best way to capture stdout from a system() command so it can be passed to another function](http://stackoverflow.com/q/125828/), [How to save text from console to a string in C (Windows platform)](http://stackoverflow.com/q/8959148/). – outis May 08 '12 at 18:07

3 Answers3

6

There's nothing like that in standard C, but usually, for compatibility with POSIX, compilers implement popen (_popen in VC++), which starts a command (as it would do with system) and returns a FILE * 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 usual fread/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 popen gives you) I would simply go with the "real" way to perform IO redirection on Windows, i.e. with CreateProcess and the appropriate options; see e.g. here.

Community
  • 1
  • 1
Matteo Italia
  • 115,256
  • 16
  • 181
  • 279
  • Awesome answer. +1 .... Have a look at my answer too... It solves in a simpler manner. – Tabrez Ahmed May 08 '12 at 18:04
  • @ahmedtabrez: thank you, but I wouldn't recommend your method, since it involves file IO (with all its cons: permissions, already existing files, dealing with errors, ...) when it's not necessary. – Matteo Italia May 08 '12 at 18:07
2

Matteo Italia's answer is awesome. But some compilers (especially older ones) don't support popen().

If popen() is not supported, here's a possible solution.

In DOS we can redirect the output to a temporary file using >.

Ex:

C:\> ipconfig > temp.txt

I can then open temp.txt in my C-code and then reprocess its content.

My C code for this will be something like:

system("ipconfig > temp.txt");
FILE *fp;
fp = fopen("temp.txt","r");
//                                         //
//... Code for reprocessing can go here ...//
//                                         //
Tabrez Ahmed
  • 2,637
  • 6
  • 27
  • 47
1

Here's an alternative answer for those without popen that should work on most system. This code is not thread safe. I expect that this is not a significant limitation for most situation.

#include <stdio.h>
#include <process.h>
#include <io.h>
#include <sys\stat.h>
#include <assert.h>

int main()
{
    int so = dup(1);
    close(1);
    int i = creat("output.txt", S_IWRITE);
    assert(i == 1); // i should be 1...
    system("dir");
    close(i);
    dup2(so, 1);
    close(so);
}
Robin Caron
  • 617
  • 4
  • 8