- 论坛徽章:
- 0
|
我只是想看看 不加 有什么影响, 还是觉得和缓冲有关,故自己设置缓冲
- #include <stdio.h>
- int main()
- {
- char buffer[20] ="1234567890";
- FILE * fp = fopen("a.txt","r+b");
- char fbuffer[1024]="";
-
- if (setvbuf(fp, fbuffer, _IOFBF, sizeof fbuffer) != 0)
- perror("setvbuf");
- fseek(fp, 2, SEEK_CUR);
- fread( buffer,1, 10,fp);
-
- /* fseek(fp, 10, SEEK_SET);*/
- /*fflush(fp); */ /*call this func programme is run ok*/
- fprintf(fp,"%s","apple");
- fclose(fp);
-
- }
复制代码
使用gdb调试:从fseek(fp, 2, SEEK_CUR); 开始调试
执行 fseek(fp, 2, SEEK_CUR);前
p fbuffer
$1 = '\000' <repeats 1023 times>
执行 fseek(fp, 2, SEEK_CUR); 后
p fbuffer
$2= '\000' <repeats 1023 times>
执行 fread( buffer,1, 10,fp); 前
p fbuffer
$3 = "\000\000\000\000\000\000\000\00034567890abcdefghijklmnopqrstuvwxyz\n", '\000' <repeats 980 times>
执行 fprintf(fp,"%s","apple");前
p buffer
$4 = "\000\000\000\000\000\000\000\00034567890abcdefghijklmnopqrstuvwxyz\n", '\000' <repeats 980 times>
执行 fprintf(fp,"%s","apple");后
$5= "\000\000\000\000\000\000\000\00034567890abapplehijklmnopqrstuvwxyz\n", '\000' <repeats 980 times
最后关闭文件, 引起缓冲到文件
由于先fseek(2), 后读入10个字节 在写入5个字符 故先将后面的字符写入,在写入前面的字符 故得到输出结果
[ 本帖最后由 stuff990 于 2006-10-21 17:09 编辑 ] |
|