setvbuf,用於設定檔案流的緩衝區
基本介紹
- 中文名:setvbuf
- 功 能:把緩衝區與流相關
- 參數:stream :指向流的指針
- buf :期望緩衝區的地址
- 返回值:成功執行返回0,否則返回非零值
簡介,程式例,
簡介
函式名: setvbuf
用 法: int setvbuf(FILE *stream, char *buf, int type, unsigned size);
type : 期望緩衝區的類型:
_IOFBF(滿緩衝):當緩衝區為空時,從流讀入數據。或者當緩衝區滿時,向流寫入數 據。
_IOLBF(行緩衝):每次從流中讀入一行數據或向流中寫入一行數據。
_IONBF(無緩衝):直接從流中讀入數據或直接向流中寫入數據,而沒有緩衝區。
size : 緩衝區內位元組的數量。
注意:This function should be called once the file associated with the stream has already been opened but before any input or output operation has taken place.
意思是這個函式應該在打開流後,立即調用,在任何對該流做輸入輸出前
程式例
#include <stdio.h>int main(){ FILE *input, *output; char bufr[512]; input = fopen("file.in", "r+b"); output = fopen("file.out", "w"); /* set up input stream for minimal disk access, using our own character buffer */ if (setvbuf(input, bufr, _IOFBF, 512) != 0) printf("failed to set up buffer for input file\n"); else printf("buffer set up for input file\n"); /* set up output stream for line buffering using space that will be obtained through an indirect call to malloc */ if (setvbuf(output, NULL, _IOLBF, 132) != 0) printf("failed to set up buffer for output file\n"); else printf("buffer set up for output file\n"); /* perform file I/O here */ /* close files */ fclose(input); fclose(output); return 0;}