Where does the standard output go the following program

This is a discussion on Where does the standard output go the following program within the Linux Programming forums, part of the Platform Specific Boards category; Here is the input file for page.c [cdalten@localhost oakland]$ more tri.c Code: #include <stdio.h> int main(void) { printf("What's going on ...

  1. #1
    Banned
    Join Date
    May 2007
    Location
    Berkeley, CA
    Posts
    329

    Where does the standard output go the following program

    Here is the input file for page.c

    [cdalten@localhost oakland]$ more tri.c
    Code:
    #include <stdio.h>
    
    int main(void)
    {
      printf("What's going on here??!\n"); 
      return 0;
    }
    and here is page.c itself.

    [cdalten@localhost oakland]$ more page.c
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <sys/wait.h>
    
    #define PAGER "${PAGER:~more}"
    int main(int argc, char *argv[])
    {
      char line[BUFSIZ];
      FILE *fpin, *fpout;
    
      if(argc !=2) {
        fprintf(stderr, "not enough args\n");
        exit(1);
      }
    
      if((fpin = fopen(argv[1], "r")) == NULL) {
        fprintf(stderr, "fopen error\n");
        exit(1);
      }
    
      if ((fpout = popen("/bin/more", "w")) == NULL) {
        fprintf(stderr, "popen error\n");
        exit(1);
      }
    
      while (fgets(line, BUFSIZ, fpin) != NULL) {
        if (fputs(line, fpout) == EOF) {
          fprintf(stderr, "fputs error to pipe\n");
          exit(1);
        }
      }
    
      if(pclose(fpout) == -1) {
        fprintf(stderr, "pclose error\n");
        exit(1);
      }
      exit(0);
    }
    When I run it, I get...
    [cdalten@localhost oakland]$ gcc -Wall page.c -o page
    [cdalten@localhost oakland]$ ./page tri.c
    #include <stdio.h>

    int main(void)
    {
    printf("What's going on here??!\n");
    return 0;
    }
    [cdalten@localhost oakland]$

    The question is, does fputs() in page.c print the standard output in the parent or the child process?

  2. #2
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by Overworked_PhD View Post
    The question is, does fputs() in page.c print the standard output in the parent or the child process?
    In your example, fputs does not print to standard output. stdout is a i/o stream, just as fpout is. The stream fputs is writing to is fpout.

    You do not have access to the child process of popen in the sense that there is code you could include that will be executed there.

    However, from the user's perspective, the child process's stdout is indistinguishable from the parent stdout. The child process ("/bin/more") receives data from fputs via fpout -- when you write to a popen handle, you are writing to the stdin of the child process. more prints the content of argv[1] to stdout -- which it is a child process, but unless redirected stdout is to the console.

    You are correct in recognizing there are TWO stdout's in a parent/child relationship.
    Last edited by MK27; 02-23-2009 at 08:51 PM.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Client-server system with input from separate program
    By robot-ic in forum Networking/Device Communication
    Replies: 3
    Last Post: 01-16-2009, 03:30 PM
  2. Using variables in system()
    By Afro in forum C Programming
    Replies: 8
    Last Post: 07-03-2007, 01:27 PM
  3. How do you read gzip output from 'C' program?
    By brett in forum C Programming
    Replies: 5
    Last Post: 03-13-2006, 12:01 AM
  4. Base converter libary
    By cdonlan in forum C++ Programming
    Replies: 22
    Last Post: 05-15-2005, 02:11 AM
  5. Need help fixing bugs in data parsing program
    By daluu in forum C Programming
    Replies: 8
    Last Post: 03-27-2003, 06:02 PM

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