C++ 中cerr和cout的區(qū)別實(shí)例詳解
C++ 中cerr和cout的區(qū)別實(shí)例詳解
前言:
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: 錯(cuò)誤輸出流,無緩沖,不可以重定向。輸出的數(shù)據(jù)不經(jīng)過緩沖區(qū),直接放到指定的目標(biāo)中,既然不經(jīng)過緩沖區(qū)那么其它程序就無法把要輸出的內(nèi)容送到其他目標(biāo)中,所以說它不能被重定向。
cout:標(biāo)準(zhǔn)輸出流,有緩沖,可重定向。把要輸出的數(shù)據(jù)先放到緩沖區(qū)中,然后再從緩沖區(qū)到你指定的設(shè)備中。當(dāng)向cout流插入一個(gè)endl,不論緩沖區(qū)是否漫了,都立即輸出流中所有數(shù)據(jù),然后插入一個(gè)換行符.
注:Linux下可以用標(biāo)準(zhǔn)錯(cuò)誤輸出間接重定向cerr的輸出
如有疑問請留言或者到本站社區(qū)交流討論,感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!
相關(guān)文章
C++ 寫的UrlEncode和UrlDecode實(shí)例
這篇文章主要介紹了C++ 寫的UrlEncode和UrlDecode實(shí)例,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-12-12C++實(shí)現(xiàn)學(xué)生成績管理系統(tǒng)最新版
這篇文章主要為大家詳細(xì)介紹了C++實(shí)現(xiàn)學(xué)生成績管理系統(tǒng)最新版,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-06-06C語言實(shí)現(xiàn)商品管理系統(tǒng)開發(fā)
這篇文章主要為大家詳細(xì)介紹了C語言實(shí)現(xiàn)商品管理系統(tǒng)開發(fā),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-08-08C語言掃雷詳細(xì)代碼分步實(shí)現(xiàn)流程
掃雷是電腦上很經(jīng)典的游戲,特意去網(wǎng)上玩了一會(huì),幾次調(diào)試之后,發(fā)現(xiàn)這個(gè)比三子棋要復(fù)雜一些,尤其是空白展開算法上和堵截玩家有的一拼,與實(shí)際游戲差別較大,不能使用光標(biāo),下面來詳解每一步分析2022-02-02