setbuf, setbuffer, setlinebuf, setvbuf - stream buffering operations
用于更改buffering
这些调用必须在fopen
流之后,操作这些stream流之前调用。
#include <stdio.h>
void setbuf(FILE *stream, char *buf);
int setvbuf(FILE *stream, char *buf, int mode, size_t size);
//Returns: 0 if OK, nonzero on error
void setbuffer(FILE *stream, char *buf, size_t size);
void setlinebuf(FILE *stream);
setbuf
开启buffering,``buf
所代指的buffer
大小必须为BUFSIZ(定义在stdio.h),一般是fully buffered
,一些系统在与终端设备关联的时候,采用的是行缓冲
关闭buffering
,buf
为NULL即可
setvbuf
使用mode
来指定想要的类型
mode | |
---|---|
_IOFBF | fully buffered |
_IOLBF | line buffered |
_IONBF | unbuffered |
If we specify an unbuffered stream, the buf and size arguments are ignored.
If we specify fully buffered or line buffered, buf
and size
can optionally specify a buffer and its size.
If the stream is buffered andbuf
is NULL, the standard I/O library will automatically allocate its own buffer
of the appropriate size
for the stream. By appropriate size, we mean the value specified by the constant BUFSIZ.
一些C库如GNU C库,使用
stat
structure的st_blksize
来决定合适的标准IObuffer尺寸。