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,418
  • 3
  • 20
  • 36

2 Answers2

6
isatty(STDIN_FILENO)

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

Fred Foo
  • 355,277
  • 75
  • 744
  • 836
0

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

Philipp T.
  • 793
  • 4
  • 13