C++ - cerr - clog

C++ - cerr - clog

cin - 标准输入流
cout - 标准输出流
cerr - 标准错误流 (非缓冲)
clog - 标准错误流 (缓冲)

1. The Standard Error Stream (cerr)

The Standard Error Stream or cerr is a predefined object of the ostream class. C++ programs are written by humans and the humans are prone to commit errors, so the standard error stream or cerr is attached to the standard error device, which is also a display screen, just like the standard output stream cout.
Standard Error Stream or cerrostream 类的预定义对象。C++ 程序是由人编写的,并且人容易犯错误,因此,standard error stream or cerr 被附加到标准错误设备上,该设备也是一个显示屏,就像标准输出流 cout 一样。

However, the cerr is un-buffered and each stream insertion to cerr causes its output to appear immediately.
然而,cerr 是没有缓冲的,每次插入到 cerr 中的流都会导致其输出立即出现。

The cerr is also used in conjunction with the stream insertion operator << as shown in the following example.
cerr 也与流插入运算符 << 结合使用,如以下示例所示。

1.1 Example

//============================================================================
// Name        : std::cerr
// Author      : Yongqiang Cheng
// Version     : Version 1.0.0
// Copyright   : Copyright (c) 2019 Yongqiang Cheng
// Description : Hello World in C++, Ansi-style
//============================================================================

#include <iostream>

using namespace std;

int main()
{
	char str[] = "Unable to read.";

	cerr << "Error message : " << str << endl;

	return 0;
}

When the above code is compiled and executed, it produces the following result.
编译并执行上述代码后,将产生以下结果。

Error message : Unable to read.

2. The Standard Log Stream (clog)

The predefined object of the Standard Log Stream, clog is also an instance of ostream class. In order to use it in a C++ program, one must include the iostream header file.
Standard Log Stream or clog 是预定义对象也是 ostream 类的实例。为了在 C++ 程序中使用它,必须包含 iostream 头文件。

The clog object is said to be attached to the standard error device, which is also a display screen but the object clog is buffered.
据说 clog 对象已被附加到标准错误设备上,该设备也是一个显示屏,但是 clog 对象被缓冲。

This means that each insertion to clog could cause its output to be held in a buffer until the buffer is filled or until the buffer is flushed. It works in a similar manner to the standard error stream object, cerr.
这意味着每次向 clog 插入都会导致其输出保持在缓冲区中,直到缓冲区被填充或缓冲区被刷新为止。它的工作方式类似于标准错误流对象 cerr

The clog is also used in conjunction with the stream insertion operator as shown in the following example.
clog 还与流插入运算符结合使用,如以下示例所示。

2.1 Example

//============================================================================
// Name        : std::cerr
// Author      : Yongqiang Cheng
// Version     : Version 1.0.0
// Copyright   : Copyright (c) 2019 Yongqiang Cheng
// Description : Hello World in C++, Ansi-style
//============================================================================

#include <iostream>

using namespace std;

int main()
{
	char str[] = "Unable to read.";

	clog << "Error message : " << str << endl;

	return 0;
}

When the above code is compiled and executed, it produces the following result.
编译并执行上述代码后,将产生以下结果。

Error message : Unable to read.

3. cerr - clog

标准库定义了 4 个 IO 对象,处理输入时使用命名为 cin 的 istream 类型对象,这个对象是标准输入。处理输出时使用命名为 cout 的 ostream 类型对象,这个对象是标准输出。标准库还定义了另外两个 ostream 对象,分别命名为 cerrclogcerr 对象是标准错误,通常用来输出警告和错误信息给程序的使用者,而 clog 对象用于产生程序执行的一般信息。运行程序时,大部分操作系统都提供了重定向输入或者输出流的方法。利用重定向可以将这些流与所选择的文件联系起来。

std::cerr (console error) 是 ISO C++ 标准错误输出流,对应于 ISO C 标准库的 stderrcerr 对应标准错误流,用于显示错误消息。默认情况下被关联到标准输出流,但它不被缓冲,错误消息可以直接发送到显示器,而无需等到缓冲区满或者遇到 endl 时才输出。一般情况下不被重定向。

缓冲区的目的是减少刷屏的次数。不带缓冲的话,每写一个字母,就输出一个字母,然后刷屏。有了缓冲,你将看到若干句子同时出现在了屏幕上 (由内存翻新到显存,然后刷新屏幕)。

std::cerrstd::cout 不同,ISO C++ 要求当 cerr 被初始化后,cerr.flags() & unitbuf 非零 (保证流在每次输出操作后被刷新),且 cerr.tie() 返回 &coutcerr 默认和 cout 同步但无缓冲。

cout 经过缓冲后输出,默认情况下是显示器,可以重新定向。cout 流在内存中对应开辟了一个缓冲区,用来存放流中的数据。当向 cout 流插入一个 endl,不论缓冲区是否满了,都立即输出流中所有数据。

clog 流也是标准错误流,clog 中的信息存放在缓冲区,缓冲区满或者遇到 endl 时才输出。默认情况下,写到 cerr 的数据是不缓冲的。cerr 目的是在最需要它的紧急情况下,还能得到输出功能的支持。

References

https://www.studymite.com/

发布了454 篇原创文章 · 获赞 1733 · 访问量 103万+

猜你喜欢

转载自blog.csdn.net/chengyq116/article/details/104435577