什么是“cerr”?和“stderr”?

发布于 2024-09-08 17:56:28 字数 202 浏览 5 评论 0原文

它们之间有什么区别以及如何使用? 谁能给我举一些例子吗?

具体来说,在这两种情况下如何“写入”流以及如何恢复和输出(即输出到屏幕)已写入其中的文本?

另外,“屏幕”输出本身就是一个流,对吧?也许我对流还不够了解。当然,这也可以保存到文件中,我知道。所有这些都会使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

染年凉城似染瑾 2024-09-15 17:56:28

cerr 是 C++ 流,stderr 是 C 文件句柄,两者都代表标准错误输出。

您以与写入其他流和文件句柄相同的方式写入它们:

cerr << "Urk!\n";
fprintf (stderr, "Urk!\n");

我不确定在这种情况下“恢复”是什么意思,输出将转到标准错误,仅此而已。之后程序就不再关心它了。如果您的意思是如何从程序外部保存它以供以后使用,请参阅下一段。

默认情况下,它们会转到您的终端,但输出可以重定向到其他地方,例如:

run_my_prog 2>error.out

并且,是的,“屏幕”输出是一个流(或文件句柄),但这通常只是因为 stdout/cout< /code> 和 stderr/cerr 默认连接到您的“屏幕”。重定向会影响这一点,如以下情况所示,不会将任何内容写入屏幕:(

run_my_prog >/dev/null 2>&1

尽管有一些棘手的事情,例如直接写入 /dev/tty )。该片段将重定向标准输出和标准错误以转到位桶。

cerr is the C++ stream and stderr 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:

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:

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.

心凉 2024-09-15 17:56:28

它们有什么区别

stderr 是一个 FILE*,并且是标准 C 库的一部分。 cerr 是一个ostream,并且是标准 C++ 库的一部分。

此外,“屏幕”输出本身就是一个
流对吗?

是的。但实际上默认有两个流写入屏幕:stdout/cout用于正常输出,stderr >/cerr 用于错误消息。这对于重定向很有用:您可以将 stdout 重定向到文件,但屏幕上仍然显示错误消息。

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.

Also, the "screen" output is itself a
stream right?

Yes, it is. But there are actually two streams 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文