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?
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!).
#include <iostream>
usingnamespace 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;