help with system commands

Apr 12, 2008 at 5:41pm
hello all,
i was hopeing i could get some help with running a windows system command and getting the output of that command returned into a string.
string szMyString = runCommand("dir");
or something like that i no you can use
System("dir");
to get the directory output but all it does is output to the console,
is there anyway to output to a string?
Apr 12, 2008 at 8:26pm
I am pretty sure you can put the data it gives you into a file, and then get the data from the file, but I don't think you can just pipe it into a string.
Apr 15, 2008 at 8:18am
@tweak
there is nothing imposible in programming;)
ahead, save the result of dir command to a file, and you can read the file as how as you want
 
system("dir >a.txt");
Last edited on Apr 15, 2008 at 8:19am
Apr 29, 2008 at 5:01am
@tweak
U can use _popen for the same.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
	FILE* fp = _popen("dir","rt");

	if(fp != NULL)
	{
		string responseString;
		char a[100];

		while(!feof(fp))
			if(fgets(a, 100, fp) != 0)
				responseString += a;

		cout<<responseString<<endl;
	}

	_pclose(fp);
Topic archived. No new replies allowed.