View Full Version : Popen problem with stdout buffer

05-25-2004, 09:28 AM
char buffer[BUFSIZ+1];
string Process::child_Output( string input ) {
string temp;
memset(buffer, '\0', sizeof(buffer));
read = popen(input.c_str(), "r");
if(read!=NULL) {
chars_read = fread(buffer, sizeof(char), BUFSIZ, read);
if(chars_read>0) {
temp = buffer;
return temp;
return " ";

Assume I got this command to tell you what wallpaper are you using:
$ stupid

So with that function, I can save the output of stupid program to string

Process p;
string stupid_person = p.child_Output("stupid");

The problem is my stupid_person string is doomed. My stupid_person string will be like this:
I want it be like this:

_ is space character.
See the difference? I know I can do that with dirty work. I can start from the end of the string and track the " " character then finally cut it. But is there any elegant way to do this? Maybe there are function that better than
fread(buffer, sizeof(char), BUFSIZ, read);

Thank you.............

05-25-2004, 12:29 PM
Well I would normally use fgets() to read from a pipe writing lines of text, just like I would use fgets() for reading from a text file

05-26-2004, 03:00 AM
fread(buffer, sizeof(char), BUFSIZ, read);
With this function my string will be:
There is \n that miss from my sight.

Arrggggggghhhhhhhhhhhhhhh......................... .....