什么是“cerr”?和“stderr”?
它们之间有什么区别以及如何使用? 谁能给我举一些例子吗?
具体来说,在这两种情况下如何“写入”流以及如何恢复和输出(即输出到屏幕)已写入其中的文本?
另外,“屏幕”输出本身就是一个流,对吧?也许我对流还不够了解。当然,这也可以保存到文件中,我知道。所有这些都会使用 fprintf
/ fscanf
等吗?
What is the difference between them and how are they used?
Can anyone point me to examples?
Specifically, how do you "write" to the stream in both cases and how do you recover and output (i.e. to the screen) the text that had been written to it?
Also, the "screen" output is itself a stream right? Maybe I don't understand streams well enough. This can also be saved to a file of course, I know. Would all of these use fprintf
/ fscanf
, etc?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
cerr 是 C++ 流,stderr 是 C 文件句柄,两者都代表标准错误输出。
您以与写入其他流和文件句柄相同的方式写入它们:
我不确定在这种情况下“恢复”是什么意思,输出将转到标准错误,仅此而已。之后程序就不再关心它了。如果您的意思是如何从程序外部保存它以供以后使用,请参阅下一段。
默认情况下,它们会转到您的终端,但输出可以重定向到其他地方,例如:
并且,是的,“屏幕”输出是一个流(或文件句柄),但这通常只是因为
stdout/cout< /code> 和
stderr/cerr
默认连接到您的“屏幕”。重定向会影响这一点,如以下情况所示,不会将任何内容写入屏幕:(尽管有一些棘手的事情,例如直接写入
/dev/tty
)。该片段将重定向标准输出和标准错误以转到位桶。cerr
is the C++ stream andstderr
is the C file handle, both representing the standard error output.You write to them the same way you write to other streams and file handles:
I'm not sure what you mean by "recover" in this context, the output goes to standard error and that's it. The program's not meant to care about it after that. If you mean how to save it for later, from outside the program, see the next paragraph.
By default, they'll go to your terminal but the output can be redirected elsewhere with something like:
And, yes, the "screen" output is a stream (or file handle) but that's generally only because
stdout/cout
andstderr/cerr
are connected to your "screen" by default. Redirection will affect this as in the following case where nothing will be written to your screen:(tricky things like writing directly to
/dev/tty
notwithstanding). That snippet will redirect both standard output and standard error to go to the bit bucket.stderr
是一个FILE*
,并且是标准 C 库的一部分。cerr
是一个ostream
,并且是标准 C++ 库的一部分。是的。但实际上默认有两个流写入屏幕:
stdout
/cout
用于正常输出,stderr
>/cerr
用于错误消息。这对于重定向很有用:您可以将stdout
重定向到文件,但屏幕上仍然显示错误消息。stderr
is aFILE*
, and part of the standard C library.cerr
is anostream
, and part of the standard C++ library.Yes, it is. But there are actually two streams that write to the screen by default:
stdout
/cout
is for normal output andstderr
/cerr
is for error messages. This is useful for redirection: You can redirectstdout
to a file but still have your error messages on the screen.