I have a program written in c that gets invoked like:
"my_program arg1 arg2 argc < example_file".
or
"cat text_file | my_program arg1 arg2"
I have the following code below:
FILE *fp = fopen("/dev/stdin", "r");
int KMAX = 1024;
char t [KMAX];
iff (fgets(t, KMAX, fp) != NULL) {
......
This works great if the user actually supplies input. However, if I just invoke the program like:
my_program arg1 arg2 arg3
It just hangs and waits for user input. What is the best way to read from stdin in these two cases? I thought that checking against Null would work, but it doesn’t seem to work. Normally I would just check to see if the text file exists, but that would only work for the first way to execute the program and not the second. Any suggestions would be appreciated.
The easiest way is to just use isatty() on stdin.