system() help

hello, i have a quick question about system commands. i have been having trouble with this for some time....

is it possible to save the text output of a system() command? for example, would the data from system("ipconfig") or system("tasklist") be able to be saved in a char variable? or at least a string?

thanks for the help
I've tryed but it seem's to always conflict with somthing
kia might be able to help
allright...ty i'll keep checking back here
Use freopen().

Check the reference for this function here;

http://www.cplusplus.com/reference/clibrary/cstdio/freopen.html

The following short code is an example of it in use. This code redirects some of the screen output to a file called file.txt. It should compile and run, but you'll need to press a key to get it to continue processing (the output of the system("Pause") command doesn't show on screen!).

Open file.txt to see the output.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include <iostream>

using namespace std;

int main(int argc, char *argv[])
{

    // open a file called "file.txt" and send output to it
    FILE *stream ;
    if((stream = freopen("file.txt", "w", stdout)) == NULL)
       exit(-1);

    // you WON'T see the following on the screen
    cout << "this is going to a file.txt NOT the screen\n";
   
    // you WON'T see the "Press any key to continue" message either!
    system("PAUSE");
    
    // now send the output to the screen once again;
    stream = freopen("CON", "w", stdout);

   // SO you WILL see this;
    cout << "\n\nI'm back...\n";
    // and the prompt from this;
    system("Pause");

    return EXIT_SUCCESS;
}


I guess if you wanted to capture just the output of say system("ipconfig") you could do something like;

1
2
3
4
5
    FILE *stream ;
    if((stream = freopen("ipconfig.txt", "w", stdout)) == NULL)
       exit(-1);
    system("ipconfig");
    stream = freopen("CON", "w", stdout);


and then immediately open ipconfig.txt and read its contents into an array.

You could also use stringstream - see the following;

http://www.velocityreviews.com/forums/t280688-stdout-redirection.html

But I can't help you much with this as I've never used it. Sorry :(

Regards,
Muzhogg
Last edited on
How about

system("ipconfig >> flap.txt");

This will put the output of ipconfig in flap.txt. Then you could like simply read the file into a variable. It isn't the best solution, but it works.
Topic archived. No new replies allowed.