雷火电竞-中国电竞赛事及体育赛事平台

歡迎來(lái)到入門教程網(wǎng)!

C語(yǔ)言

當(dāng)前位置:主頁(yè) > 軟件編程 > C語(yǔ)言 >

C++ 中cerr和cout的區(qū)別實(shí)例詳解

來(lái)源:本站原創(chuàng)|時(shí)間:2020-01-10|欄目:C語(yǔ)言|點(diǎn)擊:

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ò)誤輸出流,無(wú)緩沖,不可以重定向。輸出的數(shù)據(jù)不經(jīng)過(guò)緩沖區(qū),直接放到指定的目標(biāo)中,既然不經(jīng)過(guò)緩沖區(qū)那么其它程序就無(wú)法把要輸出的內(nèi)容送到其他目標(biāo)中,所以說(shuō)它不能被重定向。 

cout:標(biāo)準(zhǔn)輸出流,有緩沖,可重定向。把要輸出的數(shù)據(jù)先放到緩沖區(qū)中,然后再?gòu)木彌_區(qū)到你指定的設(shè)備中。當(dāng)向cout流插入一個(gè)endl,不論緩沖區(qū)是否漫了,都立即輸出流中所有數(shù)據(jù),然后插入一個(gè)換行符. 

注:Linux下可以用標(biāo)準(zhǔn)錯(cuò)誤輸出間接重定向cerr的輸出

如有疑問(wèn)請(qǐng)留言或者到本站社區(qū)交流討論,感謝閱讀,希望能幫助到大家,謝謝大家對(duì)本站的支持!

上一篇:淺析c語(yǔ)言中的內(nèi)存

欄    目:C語(yǔ)言

下一篇:Sublime Text 3 實(shí)現(xiàn)C語(yǔ)言代碼的編譯和運(yùn)行(示例講解)

本文標(biāo)題:C++ 中cerr和cout的區(qū)別實(shí)例詳解

本文地址:http://www.jygsgssxh.com/a1/Cyuyan/1163.html

網(wǎng)頁(yè)制作CMS教程網(wǎng)絡(luò)編程軟件編程腳本語(yǔ)言數(shù)據(jù)庫(kù)服務(wù)器

如果侵犯了您的權(quán)利,請(qǐng)與我們聯(lián)系,我們將在24小時(shí)內(nèi)進(jìn)行處理、任何非本站因素導(dǎo)致的法律后果,本站均不負(fù)任何責(zé)任。

聯(lián)系QQ:835971066 | 郵箱:835971066#qq.com(#換成@)

Copyright © 2002-2020 腳本教程網(wǎng) 版權(quán)所有