3

We know how REDIRECTION of unix shells behaves as if the input from a file( eg: input.txt) was coming from the keyboard.

So I am making a cpp program that can take input through CIN either by keyboard or by text file using redirection

eg: ./a.out < input.txt

But how can I check in the c++ program whether the input being entered was from a file or typed from a keyboard?

Aditya Sanghi
  • 289
  • 1
  • 3
  • 18
  • these might help : http://stackoverflow.com/questions/8079139/c-detect-input-redirection http://stackoverflow.com/questions/2216529/check-for-unix-command-line-arguments-pipes-and-redirects-from-a-c-program http://stackoverflow.com/questions/2027484/determine-whether-process-output-is-being-redirected-in-c-c – s_b Aug 30 '14 at 14:16

1 Answers1

4

This will tell you if stdin is a terminal:

isatty(STDIN_FILENO)

http://linux.die.net/man/3/isatty

John Zwinck
  • 239,568
  • 38
  • 324
  • 436