Im writing a program that should read input via stdin, so I have the following contruct.
FILE *fp=stdin;
But this just hangs if the user hasn’t piped anything into the program, how can I check if the user is actually piping data into my program like
gunzip -c file.gz |./a.out #should work
./a.out #should exit program with nice msg.
thanks
Since you’re using file pointers, you’ll need both
isatty()andfileno()to do this:Actually, that’s the long way. The short way is to not use file pointers:
Several standard Unix programs do this check to modify their behavior. For example, if you have
lsset up to give you pretty colors, it will turn the colors off if you pipe its stdout to another program.