- 论坛徽章:
- 0
|
代码:
int main()
{
FILE *fp;
char buf[20];
if((fp=fopen("test.txt","a+"))==NULL)
{
printf("open test.txt error\n");
exit(-1);
}
fseek(fp,0,SEEK_SET);
memset(buf,0,sizeof(buf));
fread(buf,5,1,fp);
printf("buf = %s\n",buf);
memset(buf,0,sizeof(buf));
strcpy(buf,"this is a test");
printf("buf 2 = %s\n",buf);
fprintf(fp,"%s\n",buf);
fclose(fp);
exit(0);
}
几点说明:
1。文件test.txt在程序执行前已经存在,内容是:123456789。(只有这一行数据)
2。执行程序后,再看文件的内容。内容是:
123456789
12345this is a test
3。调用fseek和fread的目的纯粹是为了随意的移动文件内容指针。
问题:为什么写到文件中的内容是12345this is a test,而不是this is a test
问题出现在哪儿? |
|