Code:
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;
pclose(read);
return temp;
}
}
return " ";
}
Assume I got this command to tell you what wallpaper are you using:
$ stupid
/home/knight/wallpaper/very_stupid.png
So with that function, I can save the output of stupid program to string
Code:
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:
"/home/knight/wallpaper/very_stupid.png____________________________"
I want it be like this:
"/home/knight/wallpaper/very_stupid.png"
_ 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.............