Redirecting cout in a C++ program to a string.
Posted on 2009-04-18
I need to execute the Linux "system" call -- for example, system(" ls -al " ); but I don't want the output from it to go to the terminal. I need to capture that output in a string. It seems that cout.rdbuf could be used to do this, but I can't get it to work. popen has been suggested, but I feel like I'm throwing in the towel if cout could just be redirected for the system call and then returned to normal.
Is there a program I could test that would do this simple "ls -al" with cout redirection? Is it possibly that system uses cerr instead and I'm going to the wrong place?