Need Linux system call from C++ to send cout to a string

Youi've got some information here, but I can't get it to work. I need to call the system command on a Linux machine, and capture the output that would normally go to the terminal (cout) in a string (or stringstream).
For ex.:
prep string to receive what would normally go to cout
call system("ls -al" );
return cout to normal operation (sent to terminal)
Who is Participating?
     popen, pclose -- process I/O

     Standard C Library (libc, -lc)

     #include <stdio.h>

     FILE *
     popen(const char *command, const char *type);

     pclose(FILE *stream);

     The popen() function ``opens'' a process by creating a bidirectional pipe
     forking, and invoking the shell.  Any streams opened by previous popen()
     calls in the parent process are closed in the new child process.  Histor-
     ically, popen() was implemented with a unidirectional pipe; hence many
     implementations of popen() only allow the type argument to specify read-
     ing or writing, not both.  Since popen() is now implemented using a bidi-
     rectional pipe, the type argument may request a bidirectional data flow.
     The type argument is a pointer to a null-terminated string which must be
     `r' for reading, `w' for writing, or `r+' for reading and writing.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.