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)?
/dev/tty
是通常的方式,但是在大多数类似Unix的操作系统上,也可以从cerr
/ stderr
读取,因为系统通常会在stdin
和dup()
将其打开到stdout
和stderr
打开一次tty。
当您的stdout通过管道传输到其他位置时,在终端上显示某些内容的唯一方法(除了诸如curses和dialog之类的东西)是stderr。
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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.