gpt4 book ai didi

C++ 中cerr和cout的区别实例详解

转载 作者:qq735679552 更新时间:2022-09-28 22:32:09 25 4
gpt4 key购买 nike

CFSDN坚持开源创造价值,我们致力于搭建一个资源共享平台,让每一个IT人在这里找到属于你的精彩世界.

这篇CFSDN的博客文章C++ 中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 。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
// 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( );
}
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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的输出 。

如有疑问请留言或者到本站社区交流讨论,感谢阅读,希望能帮助到大家,谢谢大家对本站的支持! 。

原文链接:http://blog.csdn.net/z0203153008/article/details/6477456 。

最后此篇关于C++ 中cerr和cout的区别实例详解的文章就讲到这里了,如果你想了解更多关于C++ 中cerr和cout的区别实例详解的内容请搜索CFSDN的文章或继续浏览相关文章,希望大家以后支持我的博客! 。

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com