I'm making a program that receives input in 3 ways:
I'm making an if-else statement where the first part tests if there was an arg at cmd line and an else when no file was given. Now I'm just trying to find out how to make an if statement to detect whether the input is coming from file redirect. Thank you
Example of what I'm trying to do:
if(argc > 1) { //process file } else if(/*test for file redirect*/){ //process input from file redirect } else { //process standard input }
On Linux (and most other Unix-style) systems, you can do:
#include <unistd.h> bool stdin_is_redirected = isatty (0) == 0;
This relies on the fact that stdin is file descriptor 0.
stdin
Documentation here
2.1m questions
2.1m answers
60 comments
57.0k users