Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to read from stdout in C

Tags:

c

stdout

pipe

I need to write a C program (myprogram) which checks output of other programs. It should basically work like this:

./otherprogram | ./myprogram

But I could not find how to read line-by-line from stdout (or the pipe), and then write all this to stdout.

like image 940
user3155036 Avatar asked Dec 20 '22 14:12

user3155036


1 Answers

One program's stdout becomes the next program's stdin. Just read from stdin and you will be fine.

The shell, when it runs myprogram, will connect everything for you.

BTW, here is the bash code responsible: http://git.savannah.gnu.org/cgit/bash.git/tree/execute_cmd.c

Look for execute_pipeline. No, the code is not easy to follow, but it fully explains it.

like image 161
Sean Perry Avatar answered Jan 05 '23 14:01

Sean Perry