标准I/O的写缓冲区和读缓冲区是不是同一个?

fengyejushi 2014-09-27 05:01:50
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX 100
int
main()
{
FILE * fp;
int n;
char buf[MAX];
if((fp=fopen("f","r+"))==NULL)
{
perror("fopen");
exit(1);
}
n=fileno(fp);
fread(buf,sizeof(char),5,fp);
printf("%ld\n",lseek(n,0,SEEK_CUR));
printf("%ld\n",ftell(fp));
//write(n,"yui",strlen("yui"));
fwrite("lkj",sizeof(char),strlen("lkj"),fp);
printf("%ld\n",lseek(n,0,SEEK_CUR));
printf("%ld\n",ftell(fp));
return 0;
}
其中f文件的初始内容为qwerty
输出为:
7
5
7
8
最终f文件内容为qwertlkj
试想如果采用同一缓冲区f内容应为qwertylk,但书上并未显示地说明。
...全文
199 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
  • 打赏
  • 举报
回复
U would think they are the same since setvbuf only allows u to set up one buf area. I will lean to the idea that both file stream read and write share the same buffer

18,773

社区成员

发帖
与我相关
我的任务
社区描述
Linux/Unix社区 专题技术讨论区
社区管理员
  • 专题技术讨论区社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧