基于流的操作最终会调用read或者write函数进行I/O操作。为了使程序的运行效率最高,流对象通常会提供缓冲区,以减少调用系统I/O库函数的次数。
基于流的I/O提供以下3种缓冲:
全缓冲:直到缓冲区被填满,才调用系统I/O函数。对于读操作来说,直到读入的内容的字节数等于缓冲区大小或者文件已经到达结尾,才进行实际的I/O操作,将外存文件内容读入缓冲区;对于写操作来说,直到缓冲区被填满,才进行实际的I/O操作,缓冲区内容写到外存文件中。磁盘文件通常是全缓冲的。
行缓冲:直到遇到换行符'\n',才调用系统I/O库函数。对于读操作来说,遇到换行符'\n'才进行I/O操作,将所读内容读入缓冲区;对于写操作来说,遇到换行符'\n'才进行I/O操作,将缓冲区内容写到外存中。由于缓冲区的大小是有限的,所以当缓冲区被填满时,即使没有遇到换行符'\n',也同样会进行实际的I/O操作。标准输入stdin和标准输出stdout默认都是行缓冲的。
无缓冲:没有缓冲区,数据会立即读入或者输出到外存文件和设备上。标准出错stderr是无缓冲的,这样保证错误提示和输出能够及时反馈给用户,供用户排除错误。
以上3种缓冲区分别定义为3个宏,其定义如表21-1所示。
表21-1 缓冲区类型的宏定义
缓冲区类型 | 定 义 的 宏 |
全缓冲 | _IO_FULL_BUF |
行缓冲 | _IO_LINE_BUF |
无缓冲 | _IO_UNBUFFERED |
在使用上表所述的缓冲类型宏时,应将文件流对象中的缓冲区标志与该宏做"与"操作,判断结果是否为0即可知道该缓冲文件流的缓冲区是否属于该类型了。下面实例演示了得到文件流的缓冲区类型。该程序输出标准输出、标准输入和标准出错3个文件描述符的缓冲区类型、缓冲区大小等信息。
(1)在vi编辑器中编辑该程序如下:
程序清单21-1 buf.c 输出缓冲区的类型和缓冲区大小
#include <stdio.h> int main(void) { printf("stdin is "); if(stdin->_flags & _IO_UNBUFFERED) printf("unbuffered\n"); else if(stdin->_flags & _IO_LINE_BUF) printf("line-buffered\n"); else printf("fully-buffered\n"); printf("buffer size is %d\n", stdin->_IO_buf_end - stdin->_IO_buf_base); printf("discriptor is %d\n\n", fileno(stdin)); printf("stdout is "); if(stdout->_flags & _IO_UNBUFFERED) printf("unbuffered\n"); else if(stdout->_flags & _IO_LINE_BUF) printf("line-buffered\n"); else printf("fully-buffered\n"); printf("buffer size is %d\n", stdout->_IO_buf_end - stdout->_IO_buf_base); printf("discriptor is %d\n\n", fileno(stdout)); printf("stderr is "); if(stderr->_flags & _IO_UNBUFFERED) printf("unbuffered\n"); else if(stderr->_flags & _IO_LINE_BUF) printf("line-buffered\n"); else printf("fully-buffered\n"); printf("buffer size is %d\n", stderr->_IO_buf_end - stderr->_IO_buf_base); printf("discriptor is %d\n\n", fileno(stderr)); return 0; } |
(2)在shell中编译该程序如下:
(3)在shell中运行该程序如下:
$./buf stdin is line-buffered buffer size is 1024 discriptor is 0 stdout is line-buffered buffer size is 1024 discriptor is 1 stderr is unbuffered "buffer size is 0 discriptor is 2 |
(4)使用重定向后执行该程序如下:
$./buf < in.txt 1> out.txt 2> err.txt stdin is full-buffered buffer size is 4096 discriptor is stdout is full-buffered buffer size is 4096 discriptor is 1 stderr is unbuffered "buffer size is 0 discriptor is 2 |
说明:由此可知,不论什么时候,标准出错都是无缓冲的。这样可以保证出错信息及时地输出给用户,供用户排除错误、解决问题。