免费注册 查看新帖 |

Chinaunix

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

[C] 发现 windows 的 fwrite 库函数有个严重的BUG。 [复制链接]

论坛徽章:
0
31 [报告]
发表于 2008-02-23 13:28 |只看该作者
原帖由 mailt 于 2008-2-20 19:37 发表
这个...根据多年的经验总结,在下结论怀疑别人的BUG,尤其是成熟的开发库平台的BUG之前,最好还是自己先多试验一下,看看是否自己对某些东西的理解有偏差...


是这样的。

原帖由 tgbvc 于 2008-2-21 09:52 发表
果然不出我i所料,windows 的 fwrite 库函数的确存在这个BUG!然而,linux没问题,刚才试了。
强烈建议把此帖加精!!!!!!!!


在断言它是一个bug之前,不妨让我们看看 man fopen 的有关描述:
[quote]
       Reads and writes may be intermixed on read/write streams  in  any  order.
       Note  that  ANSI  C  requires  that a file positioning function intervene
       between output and input, unless an input  operation  encounters  end-of-
       file.   (If  this  condition is not met, then a read is allowed to return
       the result of writes other than the most recent.)  Therefore it  is  good
       practice  (and  indeed sometimes necessary under Linux) to put an fseek()
       or fgetpos() operation between  write  and  read  operations  on  such  a
       stream.   This  operation  may be an apparent no-op (as in fseek(..., 0L,
       SEEK_CUR) called for its synchronizing side effect.
[/quote]

因此,你的程序中如果没有遵从 C 标准在 fopen 函数上使用的要求,即在读写转换的时候没有进行文件定位操作,那么出现什么问题也怨不到编译器上去。

你提到的两个编译器的实现尽管对待 错误程序 的行为不一致,但是据此就断言其中之一有 BUG 是不正确的。其实,它们都是符合 C 标准的,在 fopen 上不存在什么 BUG;是刚开始的时候你的程序没有写正确罢了(或者说只能在特定的系统上得到正确的结果)。

论坛徽章:
0
32 [报告]
发表于 2008-02-23 14:33 |只看该作者
msdn上早就声明了这不是一个bug, 楼主你out了。

论坛徽章:
0
33 [报告]
发表于 2008-02-23 17:43 |只看该作者

回复 #1 tgbvc 的帖子

lz 还是仔细看看自己写的程序吧,明明是你的程序问题,就不要说是 微软的bug 了

论坛徽章:
0
34 [报告]
发表于 2008-02-23 19:25 |只看该作者
这帮迂 !!!  标准是人定的, 难道就没有错误!  MS做的都正确么, MS放个屁对你们都是香的....
跟你们这帮迂争辩是在浪费时间.

论坛徽章:
0
35 [报告]
发表于 2008-02-23 19:52 |只看该作者
让我想起了 "****程序员的悲哀" 的帖子.

论坛徽章:
0
36 [报告]
发表于 2008-02-23 20:22 |只看该作者
太牛了

论坛徽章:
0
37 [报告]
发表于 2008-02-23 20:34 |只看该作者
看到了现在  收益非浅

论坛徽章:
0
38 [报告]
发表于 2008-02-23 23:31 |只看该作者
if(strlen(buf) != fwrite(buf, 1, strlen(buf), stream))
   {
       printf( "Error!\n" );
       goto  MAIN_GT1;
   }
看到这里就不大想看下去 为什么要用 strlen 虽然是 "w+" 没有 "b"之类,但看着不爽快

论坛徽章:
0
39 [报告]
发表于 2008-02-23 23:32 |只看该作者
对不起了,是fwrite.看错

论坛徽章:
0
40 [报告]
发表于 2008-02-24 00:27 |只看该作者
原帖由 cjaizss 于 2008-2-20 17:40 发表
无论如何,标准I/O的输入输出之间要刷新流,这是C语言标准的规定。


严重同意:

7.19.5.3 The fopen function
Synopsis
1
#include <stdio.h>
FILE *fopen(const char * filename, const char * mode);
Description
...
6 When a file is opened with update mode (’+’ as the second or third character in the
above list of mode argument values), both input and output may be performed on the
associated stream. However, output shall not be directly followed by input without an
intervening call to the fflush function or to a file positioning function (fseek,
fsetpos, or rewind), and input shall not be directly followed by output without an
intervening call to a file positioning function, unless the input operation encounters endof-
file. Opening (or creating) a text file with update mode may instead open (or create) a
binary stream in some implementations.
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP