2

Possible Duplicate:
Detect if stdin is a terminal or pipe in C/C++/Qt?

I'm writing a command line application that expects data as either a command line argument, or from cin.

Is there a way to check if the user piped some data in the application ($ ./myapp < test.txt), and only display a prompt for keyboard input if not?

If I'm checking for !cin.good() / cin.eof() etc., the prompt will never appear.

Community
  • 1
  • 1
zyndor
  • 1,378
  • 2
  • 19
  • 36

2 Answers2

6
isatty(STDIN_FILENO)

will return whether standard input is a terminal (tty), i.e. interactive.

Fred Foo
  • 342,876
  • 71
  • 713
  • 819
0

Perhaps you can do something with fstat(2) and S_ISFIFO?

Philipp T.
  • 775
  • 4
  • 13