Popen problem with stdout buffer

This is a discussion on Popen problem with stdout buffer within the Linux Programming forums, part of the Platform Specific Boards category; Code: char buffer[BUFSIZ+1]; string Process::child_Output( string input ) { string temp; memset(buffer, '

Thread: Popen problem with stdout buffer

', sizeof(buffer)); read = popen(input.c_str(), "r"); if(read!=NULL) { ...

  1. #1
    Registered User gandalf_bar's Avatar
    Join Date
    Oct 2003
    Posts
    92

    Popen problem with stdout buffer

    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.............
    Last edited by gandalf_bar; 05-25-2004 at 10:30 AM.
    A man asked, "Who are you?"
    Buddha answered, "I am awaked."

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,855
    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
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User gandalf_bar's Avatar
    Join Date
    Oct 2003
    Posts
    92
    fread(buffer, sizeof(char), BUFSIZ, read);
    With this function my string will be:
    /home/knight/wallpaper/very_stupid.png\n
    There is \n that miss from my sight.

    Arrggggggghhhhhhhhhhhhhhh......................... .....
    A man asked, "Who are you?"
    Buddha answered, "I am awaked."

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Pipe problem, popen(), pipe().
    By apacz in forum C Programming
    Replies: 7
    Last Post: 06-08-2006, 01:55 AM
  2. Buffer problem
    By Narcose in forum Game Programming
    Replies: 3
    Last Post: 03-24-2006, 09:29 AM
  3. Having Buffer Problems With Overlapped I/O --
    By Sargera in forum C++ Programming
    Replies: 0
    Last Post: 02-07-2006, 04:46 PM
  4. popen problem
    By rozner in forum C Programming
    Replies: 2
    Last Post: 04-17-2004, 01:40 PM
  5. Tetris Questions
    By KneeGrow in forum Game Programming
    Replies: 19
    Last Post: 10-28-2003, 11:12 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21