capture prompt output

Hi all,

i am writing a simple program but i have a little problem.

I must lanch a simple system("...");

example
system("dir");

Automatic prompt execute a normal command "dir" e show output in prompt's window. I need to capture the output of command dir (or similar command) and put it in a string var or similar.

Could you help me?

p.s. sorry for my english :)
Last edited on
Look at the popen() function.
using system is frowned up for a number of reasons, but if we are throwing caution to the wind you could redirect the output of the system call to a file, and then read that file from within your program like so.

1
2
3
4
5
6
7
8
/* snippet */
std::string file_input;
system("dir > /path/to/temp/file");
fs.open("/path/to/temp/file", std::fstream::in);   
while(fs.good()) {
       file_input.append(fs.get());
}
/* snippet */


of course you'll need to include the proper headers and what not.
thank you for help,
now i read the specific of this function and i think that it is what i am finding.

Tomorrow i'll try it and i'll post the results. :D

thank you guys!!! :D

i have tried _popen() and it is ok!!!

now i have a second problem :(

i have made a socket by WinSock and until here it is all ok
but if i sent a string from server to client when i'll read it the output is corruption in ASCII

example

if i send "dir", client's printf is "à'2" but in system(...) it exacute right...i think that it is a conversion problem from byte to Ascii.

what do you think? :)
Thanks xandi for the info.
Ok,

my stupid mistake... i can't use "printf" to string but "cout"....
Topic archived. No new replies allowed.