Hi, I need to get the output of a program called with the system() function. How would I do this?

Well, that gonna be difficult using the system function, since the function doesn’t return anything expect the status i.e 0 or 1.

What you could do is pipe the output to a file and access that file to get the output of that program.

Or you will have to fork a new process and run the program in that new process and pipe or redirect the output of the child process as an input to the parent process.

ssharish

How could I "fork a new process and run the program in that new process and pipe or redirect the output of the child process as an input to the parent process"?

You perhaps have a look into a concept called Inter Process Communication (IPC) Have a look at the following tutorial.

You create a new process by call a function called fork() and you start a new program within that process using a exec family functions.

Have a look at this link

http://www.ecst.csuchico.edu/~beej/guide/ipc/

ssharish

FILE *fp = popen("myprog.exe", "r" );
while ( fgets( buff, sizeof buff, fp ) != NULL ) {
  // do stuff with lines
}
pclose( fp );

Use _popen() on windows.

Comments
I'll make it +9 then
You deserve more than just +1 for all the help you give, but here you go.

It has nothing to do with IPC.
Just use process inheritance (CP)

He did ask, on how to create process and stuff. The link should give him an idea. Perhaps Salem's solution seems the best!

ssharish

This question has already been answered. Start a new discussion instead.