I know it's probably something stupid, but, can I read a line from the stdout? I'm working under GNU/Linux.
This is a discussion on Reading from output within the C Programming forums, part of the General Programming Boards category; I know it's probably something stupid, but, can I read a line from the stdout? I'm working under GNU/Linux....
I know it's probably something stupid, but, can I read a line from the stdout? I'm working under GNU/Linux.
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.
> 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?
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.
No, it's not a pipe, anyway, I knew that it wasn't logic, but I wasn't totally sure. Thanks you anyway.