複製鏈接
請複製以下鏈接發送給好友

setvbuf

鎖定
setvbuf,用於設定文件流的緩衝區
中文名
setvbuf
功 能
把緩衝區與流相關
參    數
stream :指向流的指針
buf
期望緩衝區的地址
返回值
成功執行返回0,否則返回非零值

目錄

setvbuf簡介

函數名: 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.
意思是這個函數應該在打開流後,立即調用,在任何對該流做輸入輸出前

setvbuf程序例

#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;
}