C++ 中cerr和cout的區(qū)別實例詳解
C++ 中cerr和cout的區(qū)別實例詳解
前言:
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: 錯誤輸出流,無緩沖,不可以重定向。輸出的數據不經過緩沖區(qū),直接放到指定的目標中,既然不經過緩沖區(qū)那么其它程序就無法把要輸出的內容送到其他目標中,所以說它不能被重定向。
cout:標準輸出流,有緩沖,可重定向。把要輸出的數據先放到緩沖區(qū)中,然后再從緩沖區(qū)到你指定的設備中。當向cout流插入一個endl,不論緩沖區(qū)是否漫了,都立即輸出流中所有數據,然后插入一個換行符.
注:Linux下可以用標準錯誤輸出間接重定向cerr的輸出
如有疑問請留言或者到本站社區(qū)交流討論,感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!
上一篇:淺析c語言中的內存
欄 目:C語言
下一篇:Sublime Text 3 實現C語言代碼的編譯和運行(示例講解)
本文標題:C++ 中cerr和cout的區(qū)別實例詳解
本文地址:http://mengdiqiu.com.cn/a1/Cyuyan/1163.html
您可能感興趣的文章
- 04-02func函數+在C語言 func函數在c語言中
- 04-02c語言中對數函數的表達式 c語言中對數怎么表達
- 04-02c語言沒有round函數 round c語言
- 04-02C語言中怎么打出三角函數 c語言中怎么打出三角函數的值
- 01-10深入理解C++中常見的關鍵字含義
- 01-10使用C++實現全排列算法的方法詳解
- 01-10深入Main函數中的參數argc,argv的使用詳解
- 01-10APUE筆記之:進程環(huán)境詳解
- 01-10c++中inline的用法分析
- 01-10如何尋找數組中的第二大數


閱讀排行
本欄相關
- 04-02c語言函數調用后清空內存 c語言調用
- 04-02func函數+在C語言 func函數在c語言中
- 04-02c語言的正則匹配函數 c語言正則表達
- 04-02c語言用函數寫分段 用c語言表示分段
- 04-02c語言中對數函數的表達式 c語言中對
- 04-02c語言編寫函數冒泡排序 c語言冒泡排
- 04-02c語言沒有round函數 round c語言
- 04-02c語言分段函數怎么求 用c語言求分段
- 04-02C語言中怎么打出三角函數 c語言中怎
- 04-02c語言調用函數求fibo C語言調用函數求
隨機閱讀
- 01-10C#中split用法實例總結
- 08-05dedecms(織夢)副欄目數量限制代碼修改
- 01-10SublimeText編譯C開發(fā)環(huán)境設置
- 01-11Mac OSX 打開原生自帶讀寫NTFS功能(圖文
- 08-05DEDE織夢data目錄下的sessions文件夾有什
- 08-05織夢dedecms什么時候用欄目交叉功能?
- 01-10delphi制作wav文件的方法
- 01-11ajax實現頁面的局部加載
- 01-10使用C語言求解撲克牌的順子及n個骰子
- 04-02jquery與jsp,用jquery