cerr与cout区别实例如何详细解析?
- 内容介绍
- 文章标签
- 相关推荐
本文共计811个文字,预计阅读时间需要4分钟。
C++ 中 cerr 和 cout 的区别实例详解:
前言:cerr 和 cout 都用于输出信息,但它们在处理输出方式上有所不同。
cerr:cerr 是一个用于标准错误输出的流对象,它控制无缓冲的插入到标准错误输出作为字节流。一旦对象被构造,cerr.flags 的 unitbuf 成员将非零。这意味着 cerr 会自动将输出刷新到标准错误。
示例:cpp#include
int main() { // cerr 自动刷新输出到标准错误 std::cerr << 错误信息: 这是一条错误信息 < return 0;}
cout:cout 是一个用于标准输出的流对象,它通常用于控制台输出。cout 默认情况下是缓冲的,这意味着输出会累积在内存中直到一定量或遇到某些字符(如换行符)时才会被刷新到控制台。
示例:cpp#include
int main() { // cout 缓冲输出到标准输出 std::cout << 控制台信息: 这是一条控制台信息 < return 0;} 总结:- cerr 用于标准错误输出,输出无缓冲。- cout 用于标准输出,输出缓冲。- cerr 在输出时总是立即刷新,而 cout 通常在缓冲区满或遇到换行符时才刷新。 C++ 中cerr和cout的区别实例详解 前言: cerrThe object controls unbuffered insertions to the standard error output as a byte stream. Once the object is nstructed, the expression cerr.flags & unitbuf is nonzero. Example
// iostream_cerr.cpp
// compile with: /EHsc
// By default, cerr and clog are the same as cout
#include <iostream>
#include <fstream>
using namespace std;
void TestWide( )
{
int i = 0;
wcout << L"Enter a number: ";
wcin >> i;
wcerr << L"test for wcerr" << endl;
wclog << L"test for wclog" << endl;
}
int main( )
{
int i = 0;
cout << "Enter a number: ";
cin >> i;
cerr << "test for cerr" << endl;
clog << "test for clog" << endl;
TestWide( );
}
Input
Sample Output
Enter a number: 3
test for cerr
test for clog
Enter a number: 1
test for wcerr
test for wclogcout
The object controls insertions to the standard output as a byte stream.
cerr
extern ostream cerr;
The object controls unbuffered insertions to the standard error output as a byte stream. Once the object is constructed, the expression cerr.flags() & unitbuf is nonzero.
cout
extern ostream cout;
The object controls insertions to the standard output as a byte stream.
cerr: 错误输出流,无缓冲,不可以重定向。输出的数据不经过缓冲区,直接放到指定的目标中,既然不经过缓冲区那么其它程序就无法把要输出的内容送到其他目标中,所以说它不能被重定向。 cout:标准输出流,有缓冲,可重定向。把要输出的数据先放到缓冲区中,然后再从缓冲区到你指定的设备中。当向cout流插入一个endl,不论缓冲区是否漫了,都立即输出流中所有数据,然后插入一个换行符. 注:Linux下可以用标准错误输出间接重定向cerr的输出 如有疑问请留言或者到本站社区交流讨论,感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!
本文共计811个文字,预计阅读时间需要4分钟。
C++ 中 cerr 和 cout 的区别实例详解:
前言:cerr 和 cout 都用于输出信息,但它们在处理输出方式上有所不同。
cerr:cerr 是一个用于标准错误输出的流对象,它控制无缓冲的插入到标准错误输出作为字节流。一旦对象被构造,cerr.flags 的 unitbuf 成员将非零。这意味着 cerr 会自动将输出刷新到标准错误。
示例:cpp#include
int main() { // cerr 自动刷新输出到标准错误 std::cerr << 错误信息: 这是一条错误信息 < return 0;}
cout:cout 是一个用于标准输出的流对象,它通常用于控制台输出。cout 默认情况下是缓冲的,这意味着输出会累积在内存中直到一定量或遇到某些字符(如换行符)时才会被刷新到控制台。
示例:cpp#include
int main() { // cout 缓冲输出到标准输出 std::cout << 控制台信息: 这是一条控制台信息 < return 0;} 总结:- cerr 用于标准错误输出,输出无缓冲。- cout 用于标准输出,输出缓冲。- cerr 在输出时总是立即刷新,而 cout 通常在缓冲区满或遇到换行符时才刷新。 C++ 中cerr和cout的区别实例详解 前言: cerrThe object controls unbuffered insertions to the standard error output as a byte stream. Once the object is nstructed, the expression cerr.flags & unitbuf is nonzero. Example
// iostream_cerr.cpp
// compile with: /EHsc
// By default, cerr and clog are the same as cout
#include <iostream>
#include <fstream>
using namespace std;
void TestWide( )
{
int i = 0;
wcout << L"Enter a number: ";
wcin >> i;
wcerr << L"test for wcerr" << endl;
wclog << L"test for wclog" << endl;
}
int main( )
{
int i = 0;
cout << "Enter a number: ";
cin >> i;
cerr << "test for cerr" << endl;
clog << "test for clog" << endl;
TestWide( );
}
Input
Sample Output
Enter a number: 3
test for cerr
test for clog
Enter a number: 1
test for wcerr
test for wclogcout
The object controls insertions to the standard output as a byte stream.
cerr
extern ostream cerr;
The object controls unbuffered insertions to the standard error output as a byte stream. Once the object is constructed, the expression cerr.flags() & unitbuf is nonzero.
cout
extern ostream cout;
The object controls insertions to the standard output as a byte stream.
cerr: 错误输出流,无缓冲,不可以重定向。输出的数据不经过缓冲区,直接放到指定的目标中,既然不经过缓冲区那么其它程序就无法把要输出的内容送到其他目标中,所以说它不能被重定向。 cout:标准输出流,有缓冲,可重定向。把要输出的数据先放到缓冲区中,然后再从缓冲区到你指定的设备中。当向cout流插入一个endl,不论缓冲区是否漫了,都立即输出流中所有数据,然后插入一个换行符. 注:Linux下可以用标准错误输出间接重定向cerr的输出 如有疑问请留言或者到本站社区交流讨论,感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!

