0

Possible Duplicate:
How to execute a command and get output of command within C++?

In my c++ program, I'm running an external program using execv/system.

I would like to redirect the output of the program to a buffer in my program,i.e.:

char* buff[some size large enough for any possible output];
system(some program);
//THE PROGRAM I JUST RAN IS A SPECIAL BLACK BOX PROGRAM THAT RUNS
//AND PARSE AN HTTP DUMP FILE AND PRINTS IT TO THE SCREEN WHEN RUNNING
//I WANT THE OUTPUT TO GO A BUFFER IN MY PROGRAM.
//THE IDEA IS TO HAVE THE DATA OF THE OUTPUT ON THE BUFFER RATHER THAN ON A FILE

//have the buffer filled with the output of the program

Thanks :-)

Community
  • 1
  • 1
Alon_T
  • 1,430
  • 4
  • 26
  • 47
  • 3
    search next time: http://stackoverflow.com/questions/478898/how-to-execute-a-command-and-get-output-of-command-within-c – Mike Corcoran Nov 01 '12 at 16:28

2 Answers2

0

I am not sure if there is some other way to do this , but this what comes to my mind

char* buff[some size large enough for any possible output];
system(some program >somefile.txt);  

//have the buffer filled with the output of the program
// And redirect it into a file


//Do initial validity checks and open the file



read(somefile.txt,buff,bufflen);
//Read the file

unlink somefile.txt 
//Unlink if the file is not needed

Replace bufflen by the number of bytes you want to read from the file.

Desert Ice
  • 4,461
  • 5
  • 31
  • 58
  • Or even just read it on the stream and into a string and then you don't need to worry about sizes (If you are going to write an external file like that) – Caribou Nov 01 '12 at 16:33
0

If you want to read the output of a sub-process then (in a POSIX/UNIX/Linux world at least) you should really be looking at the use of pipes for inter-process communication. A helpful question/answer on Stackoverflow with links to some good resources is here: fork(), pipe() and exec() process creation and communication

Community
  • 1
  • 1
Neil Winton
  • 286
  • 1
  • 5