浅谈标准I/O缓冲区

转自:http://blog.csdn.net/tennysonsky/article/details/43490985


标准I/O库提供缓冲的目的是尽可能地减少使用read和write调用的次数。它也对每个I/O流自动地进行缓冲管理,从而避免了应用程序需要考虑这一点所带来的麻烦。不幸的是,标准I/O库最令人迷惑的也是它的缓冲。


标准I/O提供了三种类型的缓冲:

1、全缓冲:

在填满标准I/O缓冲区后才进行实际I/O操作。常规文件(如普通文本文件)通常是全缓冲的。


2、行缓冲:

当在输入和输出中遇到换行符时,标准I/O库执行I/O操作。这允许我们一次输出一个字符,但只有在写了一行之后才进行实际I/O操作。标准输入和标准输出对应终端设备(如屏幕)时通常是行缓冲的。


3、不带缓冲:

用户程序每次调库函数做写操作都要通过系统调用写回内核(如系统调用函数)。标准错误输出通常是无缓冲的,这样用户程序产生的错误信息可以尽快输出到设备。


下面是各个缓冲区的验证。

全缓冲:

[cpp]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. int main(int argc, char *argv[])  
  2. {  
  3.     FILE *fp = NULL;  
  4.     // 读写方式打开,文件不存在则创建  
  5.     fp = fopen("sunplusedu.txt""w+");  
  6.     if(NULL == fp)  
  7.     {  
  8.         printf("open error\n");  
  9.         return 1;  
  10.     }  
  11.     char *str = "sunplusedu\n";  
  12.     fwrite(str, 1, strlen(str), fp);    // 往文件写内容  
  13.     while(1);   // 程序阻塞在这里  
  14.   
  15.     return 0;  
  16. }  

运行程序发现,sunplusedu.txt并没有内容。因为常规文件通常是全缓冲的,只有缓冲区满了后,才会把内容写到文件中。接下来,我们改一下上面那个例子。


[cpp]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. #include <stdio.h>  
  2. #include <string.h>  
  3.   
  4. int main(int argc, char *argv[])  
  5. {  
  6.     FILE *fp = NULL;  
  7.     // 读写方式打开,文件不存在则创建  
  8.     fp = fopen("sunplusedu.txt""w+");  
  9.     if(NULL == fp)  
  10.     {  
  11.         printf("open error\n");  
  12.         return 1;  
  13.     }  
  14.     char *str = "sunplusedu\n";  
  15.     int i = 0;  
  16.     while(i <= 512){ // 缓冲区大小不确定,i的大小只是一个调试值  
  17.         fwrite(str, 1, strlen(str), fp);    // 往文件写内容  
  18.         i++;  
  19.     }  
  20.     while(1);   // 程序阻塞在这里  
  21.   
  22.     return 0;  
  23. }  


上面的例子是循环给文件写内容,让缓冲区有填满的可能,结果发现,文件是有内容的。实际上要想成功给文件写进内容,除了缓冲区填满,还有别的方法。


1)人为关闭文件,就算缓冲区没有填满,内容也会写进文件

[cpp]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. #include <stdio.h>  
  2. #include <string.h>  
  3.   
  4. int main(int argc, char *argv[])  
  5. {  
  6.     FILE *fp = NULL;  
  7.     // 读写方式打开,文件不存在则创建  
  8.     fp = fopen("sunplusedu.txt""w+");  
  9.     if(NULL == fp)  
  10.     {  
  11.         printf("open error\n");  
  12.         return 1;  
  13.     }  
  14.     char *str = "sunplusedu\n";  
  15.     fwrite(str, 1, strlen(str), fp);    // 往文件写内容  
  16.     fclose(fp);     // 人为关闭文件,就算缓冲区没有填满,内容也会写进文件  
  17.       
  18.     while(1);   // 程序阻塞在这里  
  19.   
  20.     return 0;  
  21. }  

2)程序正常结束,就算缓冲区没有填满,没有关闭文件,内容也会写进文件。

[cpp]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. #include <stdio.h>  
  2. #include <string.h>  
  3.   
  4. int main(int argc, char *argv[])  
  5. {  
  6.     FILE *fp = NULL;  
  7.     // 读写方式打开,文件不存在则创建  
  8.     fp = fopen("sunplusedu.txt""w+");  
  9.     if(NULL == fp)  
  10.     {  
  11.         printf("open error\n");  
  12.         return 1;  
  13.     }  
  14.     char *str = "sunplusedu\n";  
  15.     fwrite(str, 1, strlen(str), fp);    // 往文件写内容  
  16.       
  17.     return 0;  
  18.     // 程序正常结束,就算缓冲区没有填满,没有关闭文件,内容也会写进文件。  
  19. }  

行缓冲:

[cpp]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. #include <stdio.h>  
  2.   
  3. int main(int argc, char *argv[])  
  4. {  
  5.     printf("hello sunplusedu");  
  6.     while(1);  
  7.       
  8.     return 0;  
  9. }  

运行这个程序,会发现 hello sunplusedu 并没有打印到屏幕上。因为标准输入和标准输出对应终端设备时通常是行缓冲的,当在输入和输出中遇到换行符时,标准I/O库执行I/O操作。如下:


[cpp]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. #include <stdio.h>  
  2.   
  3. int main(int argc, char *argv[])  
  4. {  
  5.     printf("hello sunplusedu\n");  
  6.     while(1);  
  7.       
  8.     return 0;  
  9. }  


除了遇到换行符,还有别的方法可以执行I/O操作。


1)缓冲区填满

[cpp]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. int main(int argc, char *argv[])  
  2. {  
  3.     while(1){   // 循环打印,总有缓冲区填满的可能  
  4.         printf("hello sunplusedu");  
  5.     }  
  6.     while(1);  
  7.       
  8.     return 0;  
  9. }  


2)人为刷新缓冲区

[cpp]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. #include <stdio.h>  
  2.   
  3. int main(int argc, char *argv[])  
  4. {  
  5.     printf("hello sunplusedu");  
  6.     fflush(stdout); // 人为刷新  
  7.   
  8.     while(1);  
  9.       
  10.     return 0;  
  11. }  


3)程序正常结束

[cpp]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. #include <stdio.h>  
  2.   
  3. int main(int argc, char *argv[])  
  4. {  
  5.     printf("hello sunplusedu");  
  6.       
  7.     return 0;  
  8.     // 程序正常结束  
  9. }  

不带缓冲

[cpp]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. #include <unistd.h>  
  2. #include <string.h>  
  3.   
  4. int main(int argc, char *argv[])  
  5. {  
  6.     char *str = "hello sunplusedu.com";   
  7.     // 有没有\n,缓冲区有没有填满,都没关系  
  8.     write(1, str, strlen(str)); // 往标准输出写内容  
  9.     while(1);  
  10.       
  11.     return 0;  
  12. }  



  • 3
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值