C++ 中cerr和cout的区别实例详解
C++中cerr和cout的区别实例详解
前言:
cerrTheobjectcontrolsunbufferedinsertionstothestandarderroroutputasabytestream.Oncetheobjectisnstructed,theexpressioncerr.flags&unitbufisnonzero.
Example
//iostream_cerr.cpp //compilewith:/EHsc //Bydefault,cerrandclogarethesameascout #include#include usingnamespacestd; voidTestWide() { inti=0; wcout< >i; wcerr< >i; cerr<<"testforcerr"< Input SampleOutput Enteranumber:3 testforcerr testforclog Enteranumber:1 testforwcerr testforwclogcout Theobjectcontrolsinsertionstothestandardoutputasabytestream. cerr externostreamcerr; Theobjectcontrolsunbufferedinsertionstothestandarderroroutputasabytestream.Oncetheobjectisconstructed,theexpressioncerr.flags()&unitbufisnonzero. cout externostreamcout; Theobjectcontrolsinsertionstothestandardoutputasabytestream.cerr:错误输出流,无缓冲,不可以重定向。输出的数据不经过缓冲区,直接放到指定的目标中,既然不经过缓冲区那么其它程序就无法把要输出的内容送到其他目标中,所以说它不能被重定向。
cout:标准输出流,有缓冲,可重定向。把要输出的数据先放到缓冲区中,然后再从缓冲区到你指定的设备中。当向cout流插入一个endl,不论缓冲区是否漫了,都立即输出流中所有数据,然后插入一个换行符.
注:Linux下可以用标准错误输出间接重定向cerr的输出
如有疑问请留言或者到本站社区交流讨论,感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!
声明:本文内容来源于网络,版权归原作者所有,内容由互联网用户自发贡献自行上传,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任。如果您发现有涉嫌版权的内容,欢迎发送邮件至:czq8825#qq.com(发邮件时,请将#更换为@)进行举报,并提供相关证据,一经查实,本站将立刻删除涉嫌侵权内容。