是否可以在将 cout 重定向到 outfile 的同时 cout 到终端?
我正在运行一个程序并将 cout
重定向到一个输出文件,如下所示:
./program < infile.in > outfile.o
我希望能够从命令行读取选项(“-h”或“--help”)并向终端输出帮助消息。有没有一种方法可以做到这一点,但仍然将程序其余部分的常规 cout
转到输出文件?
cout
是用于此类事情的正确对象吗?
I'm running a program and redirecting cout
to an outfile, like so:
./program < infile.in > outfile.o
I want to be able to read in an option ('-h' or '--help') from the command line and output a help message to the terminal. Is there a way I can do this but still have the regular cout
from the rest of the program go to the outfile?
Would cout
be the right object to use for such a thing?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
您应该使用 cerr 将帮助消息输出到 STDERR,该消息不包含在到 outfile.o 的重定向中。
给定
./program < infile.in > outfile.o
:如果稍后您想同时重定向 STDOUT 和 STDERR,则可以这样做
如果您只想重定向 STDERR,但允许显示 STDOUT,请使用
Bash 重定向 比大多数人意识到的更复杂,通常除了最简单的形式(“>”)之外的所有内容被忽视。
You should use cerr to output your help message to STDERR, which is not included in your redirection to outfile.o.
Given
./program < infile.in > outfile.o
:If, later on, you want to redirect both STDOUT and STDERR, you can do
If you want to redirect only STDERR, but allow STDOUT to display, use
Bash redirection is more complex than most people realize, and often everything except the simplest form (">") gets overlooked.
如果您使用的是 Linux,则可以使用伪设备
/dev/tty
输出到控制终端(如果有)。即使stderr
和stdout
都被重定向,这也将起作用。其他操作系统可能提供类似的机制。例如,
将像这样工作:
请注意,如果两个流独立缓冲,则如果它们输出到同一设备,则不一定保留相对顺序。这可以通过在适当的时间冲洗流来调整。
If you're on linux you can use the pseudo device
/dev/tty
to output to a controlling terminal (if any). This will work even ifstderr
is redirected as well asstdout
. Other operating systems may provide similar mechanisms.E.g.
Will work like this:
Note the way that the with the two streams being buffered independently the relative ordering if they are outputting to the same device is not necessarily preserved. This can be adjusted by flushing the streams at appropriate times.
<代码>~$ cmd | tee log_file 将
stdout
复制到文件和终端~$ cmd 2>log_file
将stdout
打印到终端并将stderr
打印到文件中~$ cmd | tee log_file
to dupstdout
to file and terminal~$ cmd 2>log_file
to printstdout
onto terminal andstderr
into a file您可能想将帮助消息输出到 stderr。 Stderr 通常用于非正常输出,您可以将使用段落视为此类输出。
You may like to output the help message to stderr. Stderr is generally used for non-normal output and you may consider a usage paragraph to be such output.
我所做的一件事(并不是说这总是合适的)是编写具有类似此签名的模块。
然后我可以创建 fstream 对象并将它们传入,或者传入 cout 和 cerr,无论我当时需要什么。这对于编写日志记录代码很有帮助,有时您想查看终端上发生的情况,而有时您只需要一个日志文件。
HTH。
One of the things I've done - not saying this is always appropriate - is write modules that have something like this signature.
And then I can create fstream objects and pass them in, or pass in cout and cerr, whatever I need to at that time. This can be helpful in writing logging code where sometimes you want to see on-terminal what happens, and at other times you just want a logfile.
HTH.
您应该使用 cerr 而不是 cout。使用 shell 重定向>只重定向 stdout (cout),而不重定向 stderr (cerr)。
You should use cerr instead of cout. Using shell redirection > only redirects stdout (cout), not stderr (cerr).