免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 3876 | 回复: 8
打印 上一主题 下一主题

[C] C语言文件操作问题,gcc与VC 6.0编译结果不同 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2009-05-27 17:05 |只看该作者 |倒序浏览
1 /*
* =====================================================================================
3 *
4 *       Filename:  test.c
5 *
6 *    Description:  对同一文件进行边写边读.
7 *                  反复写1个字节,然后往回读取出1个字节.
8 *
9 *        Version:  1.0
10 *        Created:  2009-05-27 16:16:07
11 *       Revision:  none
12 *       Compiler:  gcc
13 *
14 *         Author:  KuangHaiBin (khb.hnu(AT)gmail.com)
15 *        Company:  Hunan University
16 *
17 * =====================================================================================
18 */
19
20 #include "stdio.h"
21 #include "stdlib.h"
22 #include "errno.h"
23
24 int
25 main()
26 {
27   char data_writed = '0';
28   char data_readed = 0;
29   int length_bytes_readed = 0;
30
31   int   file_pos = 0; /*  文件指针偏移值 >0  */
32   char    *fp_file_name = "1.txt";
33   FILE    *fp = NULL;
34
35   fp    = fopen( fp_file_name, "wb+" );
36
37   if ( fp == NULL )
38     {
39       fprintf ( stderr, "couldn't open file '%s'; %s\n",
40                 fp_file_name, strerror(errno) );
41       exit (EXIT_FAILURE);
42     }
43
44   for (data_writed = '0' ; data_writed <= '9'; data_writed += 1)
45     {
46       /*fseek(fp, length_bytes_readed, SEEK_SET);*/
47
48       file_pos = ftell(fp); /*  获取当前文件指针偏移  */
49       printf("The file pointer is at byte %ld\n", file_pos);
50
51       fwrite(&data_writed, sizeof(char), 1, fp);
52
53       fseek(fp, length_bytes_readed, SEEK_SET); /*  回退文件指针  */
54       fread(&data_readed, sizeof(char), 1, fp);
55
56       length_bytes_readed += 1;
57       printf("data_readed = %c\n", data_readed);
58     }/*  end of for (data_writed = 'A'...)  */
59
60   if ( fclose(fp) == EOF )
61     {
62       fprintf ( stderr, "couldn't close file '%s'; %s\n",
63                 fp_file_name, strerror(errno) );
64       exit (EXIT_FAILURE);
65     }
66
67   return(0);
68 }

gcc结果:
The file pointer is at byte 0
data_readed = 0
The file pointer is at byte 1
data_readed = 1
The file pointer is at byte 2
data_readed = 2
The file pointer is at byte 3
data_readed = 3
The file pointer is at byte 4
data_readed = 4
The file pointer is at byte 5
data_readed = 5
The file pointer is at byte 6
data_readed = 6
The file pointer is at byte 7
data_readed = 7
The file pointer is at byte 8
data_readed = 8
The file pointer is at byte 9
data_readed = 9
Vc 6结果:
The file pointer is at byte 0
data_readed = 0
The file pointer is at byte 1
data_readed = 0
The file pointer is at byte 1
data_readed = 0
The file pointer is at byte 2
data_readed = 0
The file pointer is at byte 3
data_readed = 0
The file pointer is at byte 4
data_readed = 0
The file pointer is at byte 5
data_readed = 0
The file pointer is at byte 6
data_readed = 0
The file pointer is at byte 7
data_readed = 0
The file pointer is at byte 8
data_readed = 0

上面的代码中gcc的结果是正确的,vc的结果错误.
但是把代码中第46行代码注释去除,得到的结果是一样的.都正确.
第46行代码的作用是显示设置写文件指针位置.根据推算,是不需要设置的.
原因是什么呢?一直不解.

[ 本帖最后由 khb_gl 于 2009-5-27 17:29 编辑 ]

论坛徽章:
0
2 [报告]
发表于 2009-05-27 17:06 |只看该作者
楼主想说明什么

论坛徽章:
0
3 [报告]
发表于 2009-05-27 17:14 |只看该作者
原帖由 anthony1983 于 2009-5-27 17:06 发表
楼主想说明什么


第一次发帖,还不怎么熟悉

论坛徽章:
0
4 [报告]
发表于 2009-05-27 17:49 |只看该作者
首先挑个刺:
readed => read
writed => written

调试发现是 fwrite() 没有更新文件当前位置指针。

论坛徽章:
0
5 [报告]
发表于 2009-05-27 18:01 |只看该作者
原帖由 langue 于 2009-5-27 17:49 发表
首先挑个刺:
readed => read
writed => written

调试发现是 fwrite() 没有更新文件当前位置指针。


这个刺挑得好

langue 一针见血.

问题就是:
在VC 6中为什么第二次调用fwrite()失败,返回0.而不是1.
而gcc却成功.

论坛徽章:
0
6 [报告]
发表于 2009-05-28 10:07 |只看该作者
vc 2010没问题

论坛徽章:
0
7 [报告]
发表于 2009-05-28 13:51 |只看该作者
不知道是bug还是规定 反正vc6.0里面的fwrite就是这样 fread后要fseek才能再fwrite

论坛徽章:
0
8 [报告]
发表于 2009-05-28 13:52 |只看该作者
When the "r+", "w+", or "a+" access type is specified, both reading and writing are allowed (the file is said to be open for "update"). However, when you switch between reading and writing, there must be an intervening fflush, fsetpos, fseek, or rewind operation. The current position can be specified for the fsetpos or fseek operation, if desired.


这是msdn上的原文 

评分

参与人数 1可用积分 +8 收起 理由
langue + 8 精品文章

查看全部评分

论坛徽章:
0
9 [报告]
发表于 2009-05-31 14:54 |只看该作者
原帖由 皇家救星 于 2009-5-28 13:52 发表
When the "r+", "w+", or "a+" access type is specified, both reading and writing are allowed (the file is said to be open for "update". However, when you switch between reading and writing, there ...

非常感谢,明白罗.去实验一下.看来找答案还得从MSDN等官方文档下手
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP