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库,使用
statstructure的st_blksize来决定合适的标准IObuffer尺寸。
本文深入探讨了C库中的流缓冲操作,包括setbuf、setvbuf、setbuffer和setlinebuf等函数的使用方法及应用场景,帮助开发者更好地理解和运用这些功能。
597

被折叠的 条评论
为什么被折叠?



