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.

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

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

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

Thanks salem it worked

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.