日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 综合教程 >内容正文

综合教程

stderr和stdout详细解说

發布時間:2023/12/19 综合教程 38 生活家
生活随笔 收集整理的這篇文章主要介紹了 stderr和stdout详细解说 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

<cstdio>

object

stderr

FILE * stderr;

Standard error stream

The standard error stream is the defaultdestination for error messages and other diagnostic warnings. Like stdout, itis usually also directed by default to the text console (generally, on thescreen).

stderr can be used as an argument for anyfunction that takes an argument of type FILE* expecting an output stream, likefputs or fprintf.

Although in many cases both stdout andstderr are associated with the same output device (like the console),applications may differentiate between what is sent to stdout and what tostderr for the case that one of them is redirected. For example, it is frequentto redirect the regular output of a console program (stdout) to a file whileexpecting the error messages to keep appearing in the console.

It is also possible to redirect stderr tosome other destination from within a program using the freopen function.

stderr is is never fully buffered onstartup. It is library-dependent whether the stream is line buffered or notbuffered by default (see setvbuf).

stderr和stdout詳細解說

今天又查了一下fprintf,其中對第一個參數stderr特別感興趣。

int fprintf(FILE *stream,char*format,[argument]);

在此之前先區分一下:printf,sprintf,fprintf。

1,printf就是標準輸出,在屏幕上打印出一段字符串來。

2,sprintf就是把格式化的數據寫入到某個字符串中。返回值字符串的長度。

3,fprintf是用于文件操作。

原型:int fprintf(FILE *stream,char *format,[argument]);

功能:fprintf()函數根據指定的format(格式)發送信息(參數)到由stream(流)指定的文件.因此fprintf()可以使得信息輸出到指定的文件。

例子:

#include<iostream>

#include<stdio.h>

usingnamespacestd;

intmain()

{

charname[] =“lucy”;

FILE* out;

out = fopen(“output.txt”,“w”);

if(NULL != out)

{

intrel =fprintf(out,“Hello %s\n”, name);

cout<<“寫入了”<< rel <<“個字符”<< endl;

}

system(“pause”);

return0;

}

返回值:若成功則返回輸出字符數,若輸出出錯則返回負值。

好了,以上到此為止。

然后深挖stdout,stderr。

stdout, stdin, stderr的中文名字分別是標準輸出,標準輸入和標準錯誤。

當一個用戶進程被創建的時候,系統會自動為該進程創建三個數據流,也就是題目中所提到的這三個。那么什么是數據流呢(stream)?我們知道,一個程序要運行,需要有輸入、輸出,如果出錯,還要能表現出自身的錯誤。這是就要從某個地方讀入數據、將數據輸出到某個地方,這就夠成了數據流。

因此,一個進程初期所擁有的這么三個數據流,就分別是標準輸出、標準輸入和標準錯誤,分別用stdout, stdin, stderr來表示。。這3個文件分別為標準輸入(stdin)、標準輸出(stdout)、標準錯誤(stderr)。它們在<stdio.h>中聲明,大多數環境中,stdin指向鍵盤,stdout、stderr指向顯示器。之所以使用stderr,若因某種原因造成其中一個文件無法訪問,相應的診斷信息要在該鏈接的輸出的末尾才能打印出來。當輸出到屏幕時,這種處理方法尚可接受,但如果輸出到一個文件或通過管道輸出到另一個程序時,就無法接受了。若有stderr存在,即使對標準輸出進行了重定向,寫到stderr中的輸出通常也會顯示在屏幕上。比如我們在c++中使用fprintf:

fprintf(stdout,”hello world!\n”);

屏幕上將打印出”helloworld!”來。

同樣,我們使用:fread(ptr,1,10,stdin);

上面的代碼會接收用戶輸入在終端里的字符,并存在ptr中。

那么標準輸入輸出和錯誤是不是只能反應在終端里呢?答案是不是的!我們可以將標準輸入和輸出重定位到文件中:

1,我們知道,標準輸出和標準錯誤默認都是將信息輸出到終端上,那么他們有什么區別呢?讓我們來看個題目:

問題:下面程序的輸出是什么?(intel筆試2011)

int main(){

fprintf(stdout,”Hello “);

fprintf(stderr,”World!”);

return0;

}

解答:這段代碼的輸出是什么呢?,然后發現輸出是:

World!Hello

這是為什么呢?在默認情況下,stdout是行緩沖的,他的輸出會放在一個buffer里面,只有到換行的時候,才會輸出到屏幕。而stderr是無緩沖的,會直接輸出,舉例來說就是fprintf(stdout, “xxxx”) 和 fprintf(stdout,”xxxx\n”),前者會緩存,直到遇到新行才會一起輸出。而fprintf(stderr, “xxxxx”),不管有么有\n,都輸出。

2,fprintf(stderr,”Can’t open it!\n”);

fprintf(stdout, “Can’t open it!\n”);

printf(“Can’t open it!\n”);

這3句效果不是一樣啊,有什么區別嗎?

有區別。

stdout — 標準輸出設備

stderr — 標準錯誤輸出設備

兩者默認向屏幕輸出。

但如果用轉向標準輸出到磁盤文件,則可看出兩者區別。stdout輸出到磁盤文件,stderr在屏幕。

C++stderr/stdout 重定向到文件

通常,stderr和stdout被用來輸出內容顯示到屏幕,但是,有時候我們需要把這些信息寫到指定的文件,方便隨時查閱。最簡單的實現方式就是,把stderr/stdout的輸出重定向到文件。

stderr/stdout重定向到文件

這里以stderr代碼說明。

#include<stdio.h>

#include<stdlib.h>

intmain( )

{

FILE *stream = freopen(“freopen.out”, “w”, stderr );

if( stream == NULL )

fprintf( stdout, “error onfreopen\n” );

else

{

fprintf( stdout, “successfullyreassigned\n” ); fflush( stdout );

fprintf(stream, “This willGoto the file ‘freopen.out’\n” );

fprintf( stderr, “Also you can do it likethis!\n” );

fclose(stream );

}

// windwos下讀取文件 freopen.out

system( “type freopen.out” );

getchar();

return 0;

}

執行結果如下,


stderr與stdout的區別

stdout(標準輸出),輸出方式是行緩沖。輸出的字符會先存放在緩沖區,等按下回車鍵時才進行實際的I/O操作。

stderr(標準出錯),是不帶緩沖的,這使得出錯信息可以直接盡快地顯示出來。

關于緩沖的說明:

關于緩沖的說明:

類型

說明

輸出情況

滿緩沖

I/O操作只有在緩沖區被填滿之后才會進行

1.緩沖區滿

2.刷出數據 (fflush)

3.關閉文件(fclose)

行緩沖

通常只有遇到換行符時,才會執行實際的I/O操作;但緩沖區滿也會強制執行

1.遇到換行符

2.緩沖區滿

3.刷出數據 (fflush)

4.關閉文件(fclose)

無緩沖

不緩存,直接進行I/O操作

直接輸出

然而就緩沖來說,stdout與stderr沒有絕對的區別,因為緩沖類型可以設定。這里要借助setvbuf() 或setbuf() 函數。

#include<stdio.h>

#include<stdlib.h>

intmain()

{

char buf[512] = {0};

setbuf(stderr, buf);

fprintf(stderr, “It is error 1\n”);

printf(“echo 1\n”);

fprintf(stderr, “It is error 2\n”);

printf(“echo 2\n”);

fprintf(stderr, “It is error 3\n”);

fflush(stderr);

getchar();

return 0;

}

運行結果如下:


這樣,我們就可以定義緩沖區大小。緩沖區默認大小由stdio.h 頭文件中的宏 BUFSIZ定義,是512字節。另外,查閱一些資料說最小不能低于256字節,但測試例子沒有這個問題(暫時沒有深究)。

setvbuf()與setbuf()

setvbuf()函數原型如下:

intsetvbuf ( FILE * stream, char * buffer, int mode, size_t size );

setbuf()可以當作是調用setvbuf(stream,buf, buf ? _IOFBF : _IONBF, BUFSIZE);

其中, mode是聲明緩沖的類型,如下幾個:

_IOFBF

滿緩沖

_IOLBF

行緩沖

_IONBF

無緩沖

size是緩沖區大小,單位字節。

/*setvbuf example */

#include<stdio.h>

intmain ()

{

FILE *pFile=fopen(“myfile.txt”,”w”);

setvbuf ( pFile , NULL , _IOFBF , 1024 );

// File operations here

fclose (pFile);

return 0;

}

總結

以上是生活随笔為你收集整理的stderr和stdout详细解说的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。