网创优客建站品牌官网
为成都网站建设公司企业提供高品质网站建设
热线:028-86922220
成都专业网站建设公司

定制建站费用3500元

符合中小企业对网站设计、功能常规化式的企业展示型网站建设

成都品牌网站建设

品牌网站建设费用6000元

本套餐主要针对企业品牌型网站、中高端设计、前端互动体验...

成都商城网站建设

商城网站建设费用8000元

商城网站建设因基本功能的需求不同费用上面也有很大的差别...

成都微信网站建设

手机微信网站建站3000元

手机微信网站开发、微信官网、微信商城网站...

建站知识

当前位置:首页 > 建站知识

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

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

创新互联建站致力于互联网网站建设与网站营销,提供成都做网站、成都网站制作、网站开发、seo优化、网站排名、互联网营销、小程序设计、公众号商城、等建站开发,创新互联建站网站建设策划专家,为不同类型的客户提供良好的互联网应用定制解决方案,帮助客户在新的全球化互联网环境中保持优势。

前言:

 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 
#include 
 
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的输出

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


当前标题:C++中cerr和cout的区别实例详解
本文路径:http://bjjierui.cn/article/gccjji.html

其他资讯