setvbuf

函式名: setvbuf
功 能: 把緩衝區與流相關
用 法: int setvbuf(FILE *stream, char *buf, int type, unsigned size);
參數:stream :指向流的指針 ;
buf : 期望緩衝區的地址;
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(void)
{
FILE *input, *output;
char bufr&#91;512&#93;;
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;
}

相關詞條

熱門詞條

聯絡我們