如何禁用 std::cerr?
我有一个包含大量 std::cerr 的程序,它直接输出到我的终端。我想知道 std::cerr 和 std::cout 之间有什么区别。如何禁用 std::cerr
(我不希望它输出到我的屏幕)?
I got a program which contains a lot of std::cerr
, it directly outputs to my terminal. I am wondering what is the difference between std::cerr
and std::cout
. And how can I disable the std::cerr
(I don't want it output to my screen)?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
正如其他人提到的,如果这是一个类 Unix 系统,那么 2>/dev/null 将 stderr (2) 重定向到天空中的大位桶 (/dev/null)。
但这里没有人解释 stderr 和 stdout 之间的区别,所以我觉得有义务至少谈谈这个主题。
std::cout 是标准输出流。这通常是您的程序应该输出消息的地方。
std::cerr 是标准错误流。这通常用于错误消息。
因此,如果您的程序“包含大量 cerr”输出,那么可能值得看看为什么打印这么多错误消息,而不是简单地隐藏这些消息。当然,这是假设您不只是碰巧有一个程序由于某种原因向 stderr 发出大量非错误输出。
As others have mentioned, if this is a Unix-like system then 2>/dev/null redirects stderr (2) to the big bit bucket in the sky (/dev/null).
But nobody here has explained what the difference between stderr and stdout is, so I feel obligated to at least touch on the topic.
std::cout is the standard output stream. This is typically where your program should output messages.
std::cerr is the standard error stream. This is usually used for error messages.
As such, if your program "contains lots of cerr" output, then it might be worth taking a look at why so many error messages are being printed, rather than simply hiding the messages. This is assuming, of course, that you don't just happen to have a program that emits lots of non-error output to stderr for some reason.
假设该程序在 *nix 系统上执行,一种可能性是将 stderr 重定向到 /dev/null。
Assuming this program is executed on a *nix system, one possibility is to redirect stderr to /dev/null.
这篇旧新闻组帖子介绍了如何重定向。 (代码太大,无法在此处发布)。您需要使用
streambuf* rdbuf
。cerr
是表示标准错误流的ostream
类的对象。它与 cstdio 流stderr
关联。默认情况下,大多数系统将其标准错误输出设置为控制台,其中显示文本消息,尽管通常可以重定向。
因为 cerr 是 ostream 类的对象,所以我们可以使用插入运算符 (ostream::operator<<) 等将字符作为格式化数据写入其中,或者将字符写入为使用 write 成员函数等处理未格式化的数据(请参阅 ostream)。
This old newsgroup post shows how to redirect. (code is too large to post here). You need to use
streambuf* rdbuf
.cerr
is an object of classostream
that represents the standard error stream. It is associated with the cstdio streamstderr
.By default, most systems have their standard error output set to the console, where text messages are shown, although this can generally be redirected.
Because
cerr
is an object of classostream
, we can write characters to it either as formatted data using for example the insertion operator (ostream::operator<<) or as unformatted data using the write member function, among others (see ostream).2>/dev/null 就可以了。我又要补30个字了。
2>/dev/null does the trick. Once again I need to make up the 30 characters.
在许多系统中,包括Windows和Unix,有两个标准输出流:stdout和stderr。
通常,程序输出到 stdout,它可以显示在屏幕上,也可以重定向到文件:
program > output.txt
或重定向为另一个程序的输入program1 |程序2。例如,您可以通过运行
program | 使用
。grep
工具搜索程序的输出。 grep 搜索词但是,如果发生错误,并将其打印到重定向的 stdout,用户将看不到它。这就是为什么有第二个错误输出。此外,用户通常不希望将错误文本写入输出文件,或将其提供给 grep。
运行程序时,您可以将其错误输出重定向到带有
program 2>file
的文件。该文件可以是 /dev/null 或&1
,这意味着重定向到 stdout。In many systems, including Windows and Unixes, there are two standard output streams: stdout and stderr.
Normally, a program outputs to stdout, which can be either displayed on screen, or redirected to a file:
program > output.txt
or redirected as input for another programprogram1 | program2
. For example, you can search in output of your program with thegrep
tool by runningprogram | grep searchword
.However, if an error occurs, and you print it to stdout which is redirected, the user won't see it. That's why there is the second output for errors. Also the user usually doesn't want error text to be written to the output file, or be fed to grep.
When running a program, you can redirect its error output to a file with
program 2>file
. The file can be /dev/null, or&1
, which would mean redirect to stdout.