Thread: Pipes (popen) problem?

  1. #1
    Registered User
    Join Date
    Oct 2010
    Location
    Belgrade, Serbia
    Posts
    4

    Pipes (popen) problem?

    Hi. This is a bit unixy question. I'm trying to pipe output of one command to another and then process the pipe input into something more meaningful.

    This is simplified code of what I am trying:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    #define BUFFSIZE 256
    
    int main(int argc, char *argv[]){
       
       FILE *pipe;
       char cmd[BUFFSIZE];
       char buffer[BUFFSIZE];
       const char *mode = "r";
       
       strcpy(cmd, "ls -l");
    
       if ( !(pipe= popen(cmd, mode)) )
       {
          perror("Pipe error");
          exit(1);
       }
       
       int row;
       row = 1;
       
       while ( fgets(buffer, sizeof buffer, pipe));
       {
          printf("row %d: %s\n", row, buffer);
          row++;
       }
       
       pclose(pipe);
    
       return 0;
    }
    But instead off getting something like this:
    Code:
    row 1: file1.txt
    row 2: file2.txt
    row 3: file3.txt
    ...
    I am geting this:
    Code:
    file1.txt
    file2.txt
    file3.txt
    row 1: Dk
    As you can see "row 1:" is printed at the end, and instead of anything usefull, Dk (garbage) gets printed after it.

    What did I miss? I thought (f)gets gets input line by line. Is it possible that piped input doesn't contain line feed?

    Thank you in advance and sorry if the question is already solved somewhere else (couldn't find it).
    Last edited by bocke; 10-27-2010 at 10:45 PM.

  2. #2
    Registered User
    Join Date
    Jan 2010
    Posts
    34
    Code:
       while ( fgets(buffer, sizeof buffer, pipe));
       {
          printf("row %d: %s\n", row, buffer);
          row++;
       }
    May be you want to write:
    Code:
       while ( fgets(buffer, sizeof buffer, pipe)) // ; remove it!
       {
          printf("row %d: %s\n", row, buffer);
          row++;
       }

  3. #3
    Registered User
    Join Date
    Oct 2010
    Location
    Belgrade, Serbia
    Posts
    4
    Thanx for pointing the mistake. It works with "ls -l" now, but it still doesn't work with program I'm trying to pipe from.

    Does the other program that's being piped from need to have something in code to successfully pipe to another program?

  4. #4
    Registered User
    Join Date
    Jan 2010
    Posts
    34
    May be you must redirect it?
    Something like:
    Code:
    popen("myexe 2>&1","r"))
    So you can get output.
    It's a guess....

  5. #5
    Registered User
    Join Date
    Oct 2010
    Location
    Belgrade, Serbia
    Posts
    4
    I figured it out. The program writes to stderr if not given any argument (so piping from stdout doesn't work).

    Thanks for the advice, I'll probably try to pipe stderr to stdout to get it to work as it should.

    Case closed.

  6. #6
    Registered User
    Join Date
    Oct 2010
    Location
    Belgrade, Serbia
    Posts
    4

    Thumbs up

    And just to confirm it works.

    Thanx once again.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  2. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM
  3. Pipe problem, popen(), pipe().
    By apacz in forum C Programming
    Replies: 7
    Last Post: 06-08-2006, 12:55 AM
  4. Laptop Problem
    By Boomba in forum Tech Board
    Replies: 1
    Last Post: 03-07-2006, 06:24 PM
  5. Replies: 5
    Last Post: 11-07-2005, 11:34 PM