setbuffer 中文man頁面
NAME
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);
DESCRIPTION 描述
有三種類型的緩沖策略,它們是無緩沖,塊緩沖和行緩沖。當(dāng)輸出流無緩沖時(shí),信息在寫的同時(shí)出現(xiàn)于目標(biāo)文件或終端上;當(dāng)是塊緩沖時(shí),字符被暫存,然后一起寫入;當(dāng)是行緩沖時(shí),字符被暫存,直到要輸出一個(gè)新行符,或者從任何與終端設(shè)備連接的流中 (典型的是 stdin) 讀取輸入時(shí)才輸出。函數(shù) fflush(3) 可以用來強(qiáng)制提前輸出。(參見 fclose(3)) 通常所有文件都是塊緩沖的。當(dāng)文件 I/O 操作在文件上發(fā)生時(shí),將調(diào)用 malloc(3) ,獲得一個(gè)緩沖。如果流指向一個(gè)終端 (通常 stdout 都是這樣),那么它是行緩沖的。標(biāo)準(zhǔn)錯(cuò)誤流 stderr 默認(rèn)總是無緩沖的。
函數(shù) setvbuf 可以用在任何打開的流上,改變它的緩沖。參數(shù) mode 必須是下列三個(gè)宏之一:
- _IONBF
- 無緩沖
- _IOLBF
- 行緩沖
- _IOFBF
- 完全緩沖
除非是無緩沖的文件,否則參數(shù) buf 應(yīng)當(dāng)指向一個(gè)長(zhǎng)度至少為 size 字節(jié)的緩沖;這個(gè)緩沖將取代當(dāng)前的緩沖。如果參數(shù) buf 是 NULL ,只有這個(gè)模式會(huì)受到影響;下次 read 或 write 操作還將分配一個(gè)新的緩沖。函數(shù) setvbuf 只能在打開一個(gè)流,還未對(duì)它進(jìn)行任何其他操作之前使用。
其他三個(gè)函數(shù)調(diào)用是函數(shù) setvbuf 的別名,函數(shù) setbuf 與使用下列語句完全等價(jià):
- setvbuf(stream, buf, buf ? _IOFBF : _IONBF, BUFSIZ);
函數(shù) setbuffer 與此相同,但是緩沖的長(zhǎng)度由用戶決定,而不是由默認(rèn)值 BUFSIZ 決定。函數(shù) setlinebuf 與使用下列語句完全等價(jià):
- setvbuf(stream, (char *)NULL, _IOLBF, 0);
RETURN VALUE 返回值
函數(shù) setvbuf 成功執(zhí)行時(shí)返回 0。它失敗時(shí)可能返回任何值,但是當(dāng) It can return any value on failure, but returns nonzero when mode 不正確,或者不能實(shí)現(xiàn)請(qǐng)求時(shí),必須返回非零值。它在失敗時(shí)可能設(shè)置 errno 。其他函數(shù)沒有返回值。
CONFORMING TO 標(biāo)準(zhǔn)參考
函數(shù) setbuf 和 setvbuf 遵循 ANSI X3.159-1989 (``ANSI C'') 標(biāo)準(zhǔn)。
BUGS
函數(shù) setbuffer 和 setlinebuf 無法移植到 4.2BSD 之前的 BSD 版本,在 Linux 中僅在 libc 4.5.21 之后的系統(tǒng)中可用。在 4.2BSD 和 4.3BSD 系統(tǒng)中, setbuf 總是使用非最優(yōu)的緩沖大小,應(yīng)當(dāng)避免使用它。 在 stream 被關(guān)閉時(shí),必須確保 buf 和它指向的空間仍然存在。這通常發(fā)生在程序終止時(shí)。 例如,下列調(diào)用是非法的:
#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)
#p#
NAME
setbuf, 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);
DESCRIPTION
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 buffered
Except 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 VALUE
The 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 TO
The setbuf and setvbuf functions conform to ANSI X3.159-1989 (``ANSI C'').
BUGS
The 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 ALSO
fclose(3), fflush(3), fopen(3), fread(3), malloc(3), printf(3), puts(3)