setvbuf 建立緩衝區函式

2021-07-12 00:53:05 字數 1277 閱讀 1380

原型:setvbuf(file * restrict fp,char *restrict buf,int mode,size_t size);

if(setvbuf(fp,null,_iofbf,bufsize)!=0)

puts("error!");

函式建立了乙個供標準i/o函式使用的替換緩衝區,開啟檔案後,在沒有進行任何操作前,可以呼叫這個函式.由指標fp來指定流,buf指向將使用的儲存區.如果buf的值不是null就必須建立這個緩衝區size為陣列的大小,mode將選取:_iofbf(完全緩衝)等… 參考資料 如果執行成功,函式會返回零值,否則返回乙個非零值.

使用者開啟乙個檔案後,可以建立自己的檔案緩衝區,而不必使用fopen()函式開啟檔案時設定的預設緩衝區。這樣就可以讓使用者自己來控制緩衝區,包括改變緩衝區大小、定時重新整理緩衝區、改變緩衝區型別、刪除流中預設的緩衝區、為不帶緩衝區的流開闢緩衝區等。

示例**:

#include

#include

#include

#define bufsize 1024

#define size 81

int main(void)

if(setvbuf(fa,null,_iofbf,bufsize)!=0)

puts("enter name of first source file:");

while(gets(file_src)!=null && file_src[0]!='\0')

if(ferror(fa)!=0)

fprintf(stderr,"error in reding file %s\n",file_src);

if(ferror(fs)!=0)

if(fclose(fs)!=0)

puts("error!");

file++;

puts("please next file:");}}

printf("done,file is %d\n",file);

if(fclose(fa)!=0)

puts("fa file close error!");

return0;}

scanf 函式緩衝區

1當使用scanf函式接受使用者資料時,輸入的資料不會直接賦值給變數,而是首先將輸入的資料存入緩衝區,輸入的全部資料都會儲存在緩衝區中包括 空格 tab 回車 2當執行scanf函式時會先檢查緩衝區中是否有資料,如果有則直接從緩衝區取出資料賦值給變數,不會讓使用者輸入資料,如果沒有才會讓使用者從控制...

環形緩衝區的建立

建立例項 建立環形快取區 define rbuff set len 8 快取最大長度設定 typedef struct ringbuf st ringbuf st ringbuf 建立乙個ringbuf的緩衝區 void ringbuff init ringbuf st ringbuf me 初始化...

輸入緩衝區與輸出緩衝區

本博文通過一段程式來理解輸入緩衝區與輸出緩衝區。程式如下 author wanghao created time thu 17 may 2018 06 03 12 ampdt file name test.c description include int main int argc,const c...