redirect STDOUT to pipes

This is a discussion on redirect STDOUT to pipes within the Linux Programming forums, part of the Platform Specific Boards category; Hey, I'm trying to redirect stdout, so it goes into a pipe, so I can do something like this: printf("Hey ...

  1. #1
    Registered User Drogin's Avatar
    Join Date
    Oct 2005
    Location
    Norway
    Posts
    105

    redirect STDOUT to pipes

    Hey,
    I'm trying to redirect stdout, so it goes into a pipe, so I can do something like this:

    printf("Hey there!\n");
    then read from the pipe and get "Hey there!".

    The code I'm trying to use for this is the following:
    Code:
      // Redirect output to file
      int pipePair[2];
      
      if(pipe(pipePair) != 0) {
        fprintf(stderr,"Could not create pipe. Skipping command\n");
        exit(1);
      }
    
      if (dup2(pipePair[1], STDOUT_FILENO) == -1) {
        fprintf(stderr, "Error\n");
        exit(1);
      }
      
      char buffer[100];
      memset(buffer,0,100);
    
      printf("Hey there!\n");
      read(pipePair[0],buffer,3); // Read only the first 3 letters just in case
      printf("%s\n", buffer);
    Now, this code don't return.
    read() blocks, because for some reason.

    If I write manually to the pipe by saying write(pipePair[1]......blabla),
    it don't block, but it never recieves anything from stdout, so it seems the redirection failed.

    Anyone see what I do wrong?

  2. #2
    {Jaxom,Imriel,Liam}'s Dad Kennedy's Avatar
    Join Date
    Aug 2006
    Location
    Alabama
    Posts
    1,065
    Interestingly, I'm having the same issue. One that I found is that if you do a fflush(stdou) prior to the read, you'll get data.

    Also, as an aside, I can get the data instantly IFF I'm on the other side of a fork(), which is really strange.

    Something for everyone else: Is there a way to control the trigger level of a 2.6.++ pipe???

  3. #3
    Unregistered User Yarin's Avatar
    Join Date
    Jul 2007
    Posts
    1,623
    What your doing is bad practice (regardless of whether or not it works for you), because your relying on the stdout buffer to hold any data you may printf out until you get around to reading it.
    You should always be waiting to collect the data before you even start writing it. I would use a separate thread myself, but doing it from a fork as Kennedy mentioned works too.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Redirect stdout using dup
    By spank in forum Linux Programming
    Replies: 13
    Last Post: 04-03-2008, 05:16 PM
  2. Redirect stdout
    By GoLuM83 in forum C Programming
    Replies: 6
    Last Post: 12-15-2006, 03:17 PM
  3. redirect stdout to editbox
    By kerm1t in forum Windows Programming
    Replies: 1
    Last Post: 05-11-2005, 10:32 PM
  4. Problems with switch()
    By duvernais28 in forum C Programming
    Replies: 13
    Last Post: 01-28-2005, 09:42 AM
  5. redirect stdout to a file?
    By Brian in forum C Programming
    Replies: 5
    Last Post: 01-15-2002, 12:06 PM

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