- 论坛徽章:
- 0
|
本帖最后由 xfwduke 于 2013-03-06 00:35 编辑
最近在使用fopen的时候, 对文件的buffer有些疑惑.- #include<stdio.h>
- #include<stdlib.h>
- #include<stdio_ext.h>
- #include<fcntl.h>
- int main() {
- FILE *fp1 = fopen("aaaa","w");
- int sz=(int)__fbufsize(fp1);
- printf("buffer size of FILE opened via fopen:\n%d\n",sz);
- int fno = open("oooo.gz", O_RDONLY, 0644);
- FILE *fp2 = fdopen(fno,"r");
- sz=(int)__fbufsize(fp2);
- printf("buffer size of FILE opened via open and fdopen:\n%d\n",sz);
- }
复制代码 程序的结果是:
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)
|
|