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.
c++中cerr和cout的區別
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( );
}
3
1 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的輸出
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.