Reputation: 1627
I'm trying to do analysis on some data and I am building a few tools that I was hoping to chain together with pipes.
cat file.dat | ./tool | ./tool2 ...
But I'm not sure how to read the piped in data. I know it's coming in through stdin, but I can exactly wait for the file end. So I guess I'm looking for something like this:
long value;
while(scanf("%ld\n",&value)){
value2 = do_something(value);
printf("%ld\n",value2);
}
return 0;
But scanf, doesn't return 0 when stdin is "empty" what is the proper way to do this?
Upvotes: 0
Views: 71
Reputation: 41306
The function scanf
returns EOF
when it reaches the end of file. You can use a code like this to check for that:
while (1) {
long value;
int ret = scanf("%ld\n", &value);
if (ret == EOF) {
break;
}
printf("%ld\n", value);
}
See the docs for more details.
Upvotes: 1