• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

piping to C

toughwimp11

Senior member
I'm trying to make a program in c that will print the output from another program, along with some other stuff but I'm having trouble doing this

Basically, let's say I have a file test.txt which contains the text "hi"
and I have a program "abcd"

$cat test.txt | ./abcd
hi
$./abcd < test.txt
hi

More importantly, what I want is to store what was piped or sent to the C program in a string, which I can then do anything with (such as print it to stdout)

I cant seem to make this work. some of the things i've tried give me write error: Broken pipe

any suggestions on what the c syntax is?
 
1 #include <stdio.h>
2
3 int main() {
4 char buff[1024];
5 while(fgets(buff,1024,stdin)) {
6 printf("I read [%s]\n",buff);
7 }
8 return 0;
9 }

hms-daring(6)% echo "foo" | ./a.out
I read [foo
]
 
"Broken pipe" indicates that you're closing stdin in the C code before the other program has finished writing to it.

Edit: Nevermind.
 
Back
Top