Reputation: 1468
I'm writing a console application in which user interaction might be necessary (prompt for keyboard input, cli arguments etc.), but I want to keep it separate from the result of the processing (which goes to cout, in a way that it can be piped to some other application).
How can I achieve this, if I can't just send all interaction with the user to cerr (not everything is an error)?
Upvotes: 2
Views: 163
Reputation: 61439
/dev/tty
is the usual way, but it's also possible on most Unix-like operating systems to read from cerr
/stderr
because the system usually opens the tty once as stdin
and dup()
s it onto stdout
and stderr
.
Upvotes: 3
Reputation: 52314
If you need user interaction, open /dev/tty, it will be the controlling terminal for the process. Standard error and standard input may be redirected as well.
Upvotes: 1
Reputation: 793
When your stdout is piped somewhere else, the only way to show something on the terminal (apart from maybe things like curses and dialog) is stderr.
Upvotes: 1