免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1699 | 回复: 0
打印 上一主题 下一主题

[系统] 请教个fopen 文件buffer的问题 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2013-03-06 00:31 |只看该作者 |倒序浏览
本帖最后由 xfwduke 于 2013-03-06 00:35 编辑

最近在使用fopen的时候, 对文件的buffer有些疑惑.
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. #include<stdio_ext.h>
  4. #include<fcntl.h>
  5. int main() {

  6. FILE *fp1 = fopen("aaaa","w");
  7. int sz=(int)__fbufsize(fp1);
  8. printf("buffer size of FILE opened via fopen:\n%d\n",sz);

  9. int fno = open("oooo.gz", O_RDONLY, 0644);
  10. FILE *fp2 = fdopen(fno,"r");
  11. sz=(int)__fbufsize(fp2);
  12. printf("buffer size of FILE opened via open and fdopen:\n%d\n",sz);
  13. }
复制代码
程序的结果是:
buffer size of FILE opened via fopen:
0
buffer size of FILE opened via open and fdopen:
4096

想请教下
1. 为什么fopen默认不设置文件的buffer呢, 必须要在fopen后调用setvbuf么?
2. 第二种情况(先用open, 再用fdopen) 显示的4k buffer, 是fdopen的行为吧?
3. 如果确实是fopen默认不设置buffer, 而fdopen有设置, 这样做的原因是什么呢?

程序的编译/执行环境是:
Amazon VPS:
Linux ip-10-146-87-184 3.2.0-25-virtual #40-Ubuntu SMP Wed May 23 22:20:17 UTC 2012 x86_64 x86_64 x86_64 GNU/Linux
gcc version 4.6.3 (Ubuntu/Linaro 4.6.3-1ubuntu5)

您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP