setbuf, setbuffer, setlinebuf, setvbuf - 流緩衝操作
SYNOPSIS 總覽#include <stdio.h>
void setbuf(FILE *stream, char *buf);
void setbuffer(FILE *stream, char *buf, size_t size);
void setlinebuf(FILE *stream);
int setvbuf(FILE *stream, char *buf, int mode , size_t size);
有三種類型的緩衝策略,它們是無緩衝,塊緩衝和行緩衝。當輸出流無緩衝時,信息在寫的同時出現於目標文件或終端上;當是塊緩衝時,字符被暫存,然後一起寫入;當是行緩衝時,字符被暫存,直到要輸出一個新行符,或者從任何與終端設備連接的流中 (典型的是 stdin) 讀取輸入時才輸出。函數 fflush(3) 可以用來強制提前輸出。(參見 fclose(3)) 通常所有文件都是塊緩衝的。當文件 I/O 操作在文件上發生時,將調用 malloc(3) ,獲得一個緩衝。如果流指向一個終端 (通常 stdout 都是這樣),那麼它是行緩衝的。標準錯誤流 stderr 默認總是無緩衝的。
函數 setvbuf 可以用在任何打開的流上,改變它的緩衝。參數 mode 必須是下列三個宏之一:
_IONBF 無緩衝 _IOLBF 行緩衝 _IOFBF 完全緩衝除非是無緩衝的文件,否則參數 buf 應當指向一個長度至少為 size 字節的緩衝;這個緩衝將取代當前的緩衝。如果參數 buf 是 NULL ,只有這個模式會受到影響;下次 read 或 write 操作還將分配一個新的緩衝。函數 setvbuf 只能在打開一個流,還未對它進行任何其他操作之前使用。
其他三個函數調用是函數 setvbuf 的別名,函數 setbuf 與使用下列語句完全等價:
setvbuf(stream, buf, buf ? _IOFBF : _IONBF, BUFSIZ);函數 setbuffer 與此相同,但是緩衝的長度由用戶決定,而不是由默認值 BUFSIZ 決定。函數 setlinebuf 與使用下列語句完全等價:
setvbuf(stream, (char *)NULL, _IOLBF, 0); RETURN VALUE 返回值函數 setvbuf 成功執行時返回 0。它失敗時可能返回任何值,但是當 It can return any value on failure, but returns nonzero when mode 不正確,或者不能實現請求時,必須返回非零值。它在失敗時可能設置 errno 。其他函數沒有返回值。
CONFORMING TO 標準參考函數 setbuf 和 setvbuf 遵循 ANSI X3.159-1989 (``ANSI C'') 標準。
BUGS函數 setbuffer 和 setlinebuf 無法移植到 4.2BSD 之前的 BSD 版本,在 Linux 中僅在 libc 4.5.21 之後的系統中可用。在 4.2BSD 和 4.3BSD 系統中, setbuf 總是使用非最優的緩衝大小,應當避免使用它。 在 stream 被關閉時,必須確保 buf 和它指向的空間仍然存在。這通常發生在程序終止時。 例如,下列調用是非法的:
#include <stdio.h>int main(){ char buf[BUFSIZ]; setbuf(stdin, buf); printf("Hello, world!\n"); return 0;}
SEE ALSO 參見fclose(3), fflush(3), fopen(3), fread(3), malloc(3), printf(3), puts(3)
NAMEsetbuf, setbuffer, setlinebuf, setvbuf - stream buffering operations
SYNOPSIS#include <stdio.h>
void setbuf(FILE *stream, char *buf);
void setbuffer(FILE *stream, char *buf, size_t size);
void setlinebuf(FILE *stream);
int setvbuf(FILE *stream, char *buf, int mode , size_t size);
The three types of buffering available are unbuffered, block buffered, and line buffered. When an output stream is unbuffered, information appears on the destination file or terminal as soon as written; when it is block buffered many characters are saved up and written as a block; when it is line buffered characters are saved up until a newline is output or input is read from any stream attached to a terminal device (typically stdin). The function fflush(3) may be used to force the block out early. (See fclose(3).) Normally all files are block buffered. When the first I/O operation occurs on a file, malloc(3) is called, and a buffer is obtained. If a stream refers to a terminal (as stdout normally does) it is line buffered. The standard error stream stderr is always unbuffered by default.
The setvbuf function may be used on any open stream to change its buffer. The mode parameter must be one of the following three macros:
_IONBF unbuffered _IOLBF line buffered _IOFBF fully bufferedExcept for unbuffered files, the buf argument should point to a buffer at least size bytes long; this buffer will be used instead of the current buffer. If the argument buf is NULL, only the mode is affected; a new buffer will be allocated on the next read or write operation. The setvbuf function may only be used after opening a stream and before any other operations have been performed on it.
The other three calls are, in effect, simply aliases for calls to setvbuf. The setbuf function is exactly equivalent to the call
setvbuf(stream, buf, buf ? _IOFBF : _IONBF, BUFSIZ);The setbuffer function is the same, except that the size of the buffer is up to the caller, rather than being determined by the default BUFSIZ. The setlinebuf function is exactly equivalent to the call:
setvbuf(stream, (char *)NULL, _IOLBF, 0); RETURN VALUEThe function setvbuf returns 0 on success. It can return any value on failure, but returns nonzero when mode is invalid or the request cannot be honoured. It may set errno on failure. The other functions are void.
CONFORMING TOThe setbuf and setvbuf functions conform to ANSI X3.159-1989 (``ANSI C'').
BUGSThe setbuffer and setlinebuf functions are not portable to versions of BSD before 4.2BSD, and are available under Linux since libc 4.5.21. On 4.2BSD and 4.3BSD systems, setbuf always uses a suboptimal buffer size and should be avoided. You must make sure that both buf and the space it points to still exist by the time stream is closed, which also happens at program termination. For example, the following is illegal:
#include <stdio.h>int main(){ char buf[BUFSIZ]; setbuf(stdin, buf); printf("Hello, world!\n"); return 0;}
SEE ALSOfclose(3), fflush(3), fopen(3), fread(3), malloc(3), printf(3), puts(3)
【責任編輯:
韓亞珊TEL:(010)68476606】