C++ 什么是“cerr”和“stderr”?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3200117/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What are "cerr" and "stderr"?
提问by Russel
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?
另外,“屏幕”输出本身就是一个流,对吗?也许我不太了解流。这当然也可以保存到文件中,我知道。所有这些都会使用fprintf
/fscanf
等吗?
回答by paxdiablo
cerr
is the C++ stream and stderr
is the C file handle, both representing the standard error output.
cerr
是 C++ 流和stderr
C 文件句柄,都代表标准错误输出。
You write to them the same way you write to other streams and file handles:
您写入它们的方式与写入其他流和文件句柄的方式相同:
cerr << "Urk!\n";
fprintf (stderr, "Urk!\n");
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:
默认情况下,它们会转到您的终端,但输出可以重定向到其他地方,例如:
run_my_prog 2>error.out
And, yes, the "screen" output is a stream (or file handle) but that's generally only because stdout/cout
and stderr/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:
而且,是的,“屏幕”输出是一个流(或文件句柄),但这通常只是因为stdout/cout
并且stderr/cerr
默认情况下连接到您的“屏幕”。重定向会影响这一点,因为在以下情况下不会将任何内容写入您的屏幕:
run_my_prog >/dev/null 2>&1
(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.
(/dev/tty
尽管有一些棘手的事情,例如直接写入)。该代码段会将标准输出和标准错误重定向到位存储桶。
回答by dan04
What is the difference between them
它们之间有什么区别
stderr
is a FILE*
, and part of the standard C library. cerr
is an ostream
, and part of the standard C++ library.
stderr
是FILE*
标准 C 库的一部分。 cerr
是ostream
标准 C++ 库的一部分。
Also, the "screen" output is itself a stream right?
另外,“屏幕”输出本身就是一个流,对吗?
Yes, it is. But there are actually twostreams that write to the screen by default: stdout
/cout
is for normal output and stderr
/cerr
is for error messages. This is useful for redirection: You can redirect stdout
to a file but still have your error messages on the screen.
是的。但实际上有两个流在默认情况下写入屏幕:stdout
/cout
用于正常输出,stderr
/cerr
用于错误消息。这对重定向很有用:您可以重定向stdout
到文件,但屏幕上仍会显示错误消息。