免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
楼主: keanlee
打印 上一主题 下一主题

在看APUE时,做了一个例子,不懂出现问题原因! [复制链接]

论坛徽章:
0
11 [报告]
发表于 2006-10-10 14:55 |只看该作者
没错,我的运行结果:

                                                                                    
                                                                                    
123456789
this is a test
~
~
~
~

论坛徽章:
0
12 [报告]
发表于 2006-10-10 15:02 |只看该作者
Linux下测试结果

[root@linux wuqing]# more filetest.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

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);
}
[root@linux wuqing]# gcc -o filetest filetest.c
[root@linux wuqing]# more test.txt
123456789
[root@linux wuqing]# ./filetest
buf = 12345
buf 2 = this is a test
[root@linux wuqing]# more test.txt
123456789
this is a test
[root@linux wuqing]#

[ 本帖最后由 wuqing 于 2006-10-10 15:12 编辑 ]

论坛徽章:
0
13 [报告]
发表于 2006-10-10 15:04 |只看该作者
file_win

file_win.jpg (76.74 KB, 下载次数: 15)

file_win

file_win

论坛徽章:
0
14 [报告]
发表于 2006-10-10 15:09 |只看该作者
我的os是AIX。

论坛徽章:
0
15 [报告]
发表于 2006-10-10 15:11 |只看该作者
在windows下硬是加了一句:
   fseek(fp, 0, SEEK_END);
   fprintf(fp,"%s\n",buf);
才得到正常的结果
F:\test>more test.txt
123456789
this is a test

论坛徽章:
0
16 [报告]
发表于 2006-10-10 15:11 |只看该作者
[socc_3@lijian]$ xlC append.C            
[socc_3@lijian]$ a.out
buf = 12345
buf 2 = this is a test
[socc_3@lijian]$ cat test.txt
123456789
12345this is a test

论坛徽章:
0
17 [报告]
发表于 2006-10-10 15:15 |只看该作者
原帖由 keanlee 于 2006-10-10 15:11 发表
[socc_3@lijian]$ xlC append.C            
[socc_3@lijian]$ a.out
buf = 12345
buf 2 = this is a test
[socc_3@lijian]$ cat test.txt
123456789
12345this is a test


你多半是如skai所猜测的一样,strcpy写成strcat了吧

论坛徽章:
0
18 [报告]
发表于 2006-10-10 15:18 |只看该作者
[socc_3@lijian]$ more append.C
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/types.h>
#include <string.h>


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);
}
[socc_3@lijian]$
[socc_3@lijian]$ xlC append.C            
[socc_3@lijian]$ a.out
buf = 12345
buf 2 = this is a test
[socc_3@lijian]$ cat test.txt
123456789
12345this is a test

论坛徽章:
0
19 [报告]
发表于 2006-10-10 15:20 |只看该作者
是不是memset()的问题阿,数据存放区域重叠了啊,memset()不是处理不了重叠情况吗

论坛徽章:
0
20 [报告]
发表于 2006-10-10 15:21 |只看该作者
非常有意思,欢迎更多的人来测试,大家一起讨论
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP