Thread: Reading from output

  1. #1
    Registered User
    Join Date
    Dec 2005
    Posts
    7

    Reading from output

    I know it's probably something stupid, but, can I read a line from the stdout? I'm working under GNU/Linux.

  2. #2
    Rabble Rouser Slacker's Avatar
    Join Date
    Dec 2005
    Posts
    116
    Nope, that's asking the stream to go in the wrong direction. In some cases you can read from an output device, such as a terminal screen, by grabbing data from its buffer, but that's not really doable just with stdout.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > but, can I read a line from the stdout?
    You mean like a pipe, connecting the stdout of another program to the stdin of your program?

  4. #4
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    You mean something like this?
    Code:
    itsme@itsme:~/C$ cat intercept.c
    #include <stdio.h>
    #include <string.h>
    
    int main(void)
    {
      char buf[BUFSIZ], *p;
      int addnl;
    
      while(fgets(buf, sizeof(buf), stdin))
      {
        if((p = strchr(buf, '\n')))
        {
          *p = '\0';
          addnl = 1;
        }
        else
          addnl = 0;
    
        printf("Intercepted and passing on: %s\n", buf);
    
        if(addnl)
          puts(buf);
        else
          fputs(buf, stdout);
      }
    
      return 0;
    }
    Code:
    itsme@itsme:~/C$ echo "foo bar baz"
    foo bar baz
    itsme@itsme:~/C$ echo "foo bar baz" | ./intercept
    Intercepted and passing on: foo bar baz
    foo bar baz
    itsme@itsme:~/C$ cat foo.txt
    this is some text
    this is more text
    itsme@itsme:~/C$ cat foo.txt | ./intercept
    Intercepted and passing on: this is some text
    this is some text
    Intercepted and passing on: this is more text
    this is more text
    itsme@itsme:~/C$
    If you understand what you're doing, you're not learning anything.

  5. #5
    Registered User
    Join Date
    Dec 2005
    Posts
    7
    No, it's not a pipe, anyway, I knew that it wasn't logic, but I wasn't totally sure. Thanks you anyway.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Connecting input iterator to output iterator
    By QuestionC in forum C++ Programming
    Replies: 2
    Last Post: 04-10-2007, 02:18 AM
  2. Reading output generated by php in C
    By nitinmhetre in forum C Programming
    Replies: 3
    Last Post: 01-20-2007, 07:05 AM
  3. Switch statement and returning the correct output
    By slowcoder in forum C Programming
    Replies: 6
    Last Post: 09-28-2006, 04:00 PM
  4. Trying to store system(command) Output
    By punxworm in forum C++ Programming
    Replies: 5
    Last Post: 04-20-2005, 06:46 PM
  5. Problems in reading binary file
    By serena in forum C Programming
    Replies: 3
    Last Post: 04-14-2005, 03:54 AM