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: 錯誤輸出流,無緩沖,不可以重定向。輸出的數(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流插入一個endl,不論緩沖區(qū)是否漫了,都立即輸出流中所有數(shù)據(jù),然后插入一個換行符.
注:Linux下可以用標(biāo)準(zhǔn)錯誤輸出間接重定向cerr的輸出
如有疑問請留言或者到本站社區(qū)交流討論,感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!
相關(guān)文章
用pybind11封裝C++實現(xiàn)的函數(shù)庫的方法示例
這篇文章主要介紹了用pybind11封裝C++實現(xiàn)的函數(shù)庫,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-02-02
c++將數(shù)組名作為函數(shù)參數(shù)對數(shù)組元素進(jìn)行相應(yīng)的運算
這篇文章主要介紹了c++將數(shù)組名作為函數(shù)參數(shù)對數(shù)組元素進(jìn)行相應(yīng)的運算,需要的朋友可以參考下2014-05-05
C語言文字藝術(shù)之?dāng)?shù)據(jù)輸入輸出
這篇文章主要介紹了C語言文字藝術(shù)之?dāng)?shù)據(jù)輸入輸出,C語言的語句用來向計算機(jī)系統(tǒng)發(fā)出操作指令。一條語句編寫完成經(jīng)過編譯后產(chǎn)生若干條機(jī)器指2022-07-07
C++中vector<vector<int>?>的基本使用方法
vector<vector<int>?>其實就是容器嵌套容器,外層容器的元素類型是vector<int>,下面這篇文章主要給大家介紹了關(guān)于C++中vector<vector<int>?>的基本使用方法,文中通過實例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-07-07

