免费注册 查看新帖 |

Chinaunix

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

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

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2008-02-20 15:27 |只看该作者 |倒序浏览

#include <stdio.h>
#include <conio.h>
#include <string.h>

void main( void )
{
&nbsp;&nbsp;&nbsp;FILE *stream;
&nbsp;&nbsp;&nbsp;char  ch = '1';
&nbsp;&nbsp;&nbsp;char  buf[60] = "123";
&nbsp;&nbsp;&nbsp;int  n = 0;

&nbsp;&nbsp;&nbsp;stream = fopen( "./Debug/fseek.txt", "w+" );
&nbsp;&nbsp;&nbsp;if( stream == NULL )
&nbsp;&nbsp;&nbsp;{
&nbsp;&nbsp;&nbsp;&nbsp;   printf( "The file fseek.out was not opened\n" );
&nbsp;&nbsp;&nbsp;&nbsp;   return;
&nbsp;&nbsp;&nbsp;}
&nbsp;&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;if(strlen(buf) != fwrite(buf, 1, strlen(buf), stream))
&nbsp;&nbsp;&nbsp;{
&nbsp;&nbsp;&nbsp;&nbsp;   printf( "Error!\n" );
&nbsp;&nbsp;&nbsp;&nbsp;   goto  MAIN_GT1;
&nbsp;&nbsp;&nbsp;}
&nbsp;&nbsp;&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;rewind(stream);
&nbsp;&nbsp;&nbsp;printf( "Position 1: %ld\n", ftell( stream) );//[]

&nbsp;&nbsp;&nbsp;if(0 != fseek(stream, 1, SEEK_CUR))
&nbsp;&nbsp;&nbsp;{
&nbsp;&nbsp;&nbsp;&nbsp;   printf( "Error! fseek != 0 \n" );
&nbsp;&nbsp;&nbsp;&nbsp;   goto  MAIN_GT1;
&nbsp;&nbsp;&nbsp;}
&nbsp;&nbsp;&nbsp;printf( "Position 2: %ld\n", ftell( stream) );//[]


&nbsp;&nbsp;&nbsp;while(1 == fread(&ch, 1, 1, stream))
&nbsp;&nbsp;&nbsp;{
&nbsp;&nbsp;&nbsp;&nbsp;   printf( "Position 3: %ld, ch = %c\n", ftell( stream), ch );//[]

&nbsp;&nbsp;&nbsp;&nbsp;   
&nbsp;&nbsp;&nbsp;&nbsp;   if(0 != fseek(stream, -1, SEEK_CUR))
&nbsp;&nbsp;&nbsp;&nbsp;   {
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;   printf( "Error! fseek != 0 \n" );
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;   goto  MAIN_GT1;
&nbsp;&nbsp;&nbsp;&nbsp;   }
&nbsp;&nbsp;&nbsp;&nbsp;   
&nbsp;&nbsp;&nbsp;&nbsp;   printf( "Position 4: %ld\n", ftell( stream ) );//[]

&nbsp;&nbsp;&nbsp;&nbsp;   
&nbsp;&nbsp;&nbsp;&nbsp;   ch++;
&nbsp;&nbsp;&nbsp;&nbsp;   if(1 != fwrite(&ch, 1, 1, stream))
&nbsp;&nbsp;&nbsp;&nbsp;   {
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;   printf( "Error! fwrite != 0 \n" );
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;   goto  MAIN_GT1;
&nbsp;&nbsp;&nbsp;&nbsp;   }
&nbsp;&nbsp;&nbsp;&nbsp;   printf( "Position 5: %ld\n", ftell( stream ) );//[]

&nbsp;&nbsp;&nbsp;&nbsp;   //_getch();

&nbsp;&nbsp;&nbsp;&nbsp;  // goto  MAIN_GT1;

&nbsp;&nbsp;&nbsp;&nbsp;   //n++;

&nbsp;&nbsp;&nbsp;&nbsp;   
&nbsp;&nbsp;&nbsp;}
&nbsp;&nbsp;&nbsp;
MAIN_GT1:
&nbsp;&nbsp;&nbsp;fclose( stream );
}


程序创建一个名为 fseek.txt 的空文件,然后向这个文件里写“123”三个字符,然后想把“23”改为“34”,可是为什么循环不停而且文件内容象下面不断增加?

134444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444443


原来windows的fwrite库函数有个严重的BUG,这样做就没事了:


#include <stdio.h>
#include <conio.h>
#include <string.h>

void main( void )
{
&nbsp;&nbsp;&nbsp;FILE *stream;
&nbsp;&nbsp;&nbsp;char  ch = '1';
&nbsp;&nbsp;&nbsp;char  buf[60] = "123";

&nbsp;&nbsp;&nbsp;stream = fopen( "./Debug/fseek.txt", "w+" );
&nbsp;&nbsp;&nbsp;if( stream == NULL )
&nbsp;&nbsp;&nbsp;{
&nbsp;&nbsp;&nbsp;&nbsp;   printf( "The file fseek.out was not opened\n" );
&nbsp;&nbsp;&nbsp;&nbsp;   return;
&nbsp;&nbsp;&nbsp;}
&nbsp;&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;if(strlen(buf) != fwrite(buf, 1, strlen(buf), stream))
&nbsp;&nbsp;&nbsp;{
&nbsp;&nbsp;&nbsp;&nbsp;   printf( "Error!\n" );
&nbsp;&nbsp;&nbsp;&nbsp;   goto  MAIN_GT1;
&nbsp;&nbsp;&nbsp;}
&nbsp;
&nbsp;&nbsp;&nbsp;rewind( stream );

&nbsp;&nbsp;&nbsp;&nbsp;printf( "Position 1-----: %ld\n", ftell( stream) );//[]

&nbsp;&nbsp;&nbsp;while(1 == fread(&ch, 1, 1, stream))
&nbsp;&nbsp;&nbsp;{
&nbsp;&nbsp;&nbsp;&nbsp;   printf( "Position 3: %ld, ch = %c\n", ftell( stream), ch );//[]

&nbsp;&nbsp;&nbsp;&nbsp;   if(0 != fseek(stream, -1, SEEK_CUR))
&nbsp;&nbsp;&nbsp;&nbsp;   {
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;   printf( "Error! fseek != 0 \n" );
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;   goto  MAIN_GT1;
&nbsp;&nbsp;&nbsp;&nbsp;   }
&nbsp;&nbsp;&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;   
&nbsp;&nbsp;&nbsp;&nbsp;   ch++;
&nbsp;&nbsp;&nbsp;&nbsp;   if(1 != fwrite(&ch, 1, 1, stream))
&nbsp;&nbsp;&nbsp;&nbsp;   {
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;   printf( "Error! fwrite != 0 \n" );
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;   goto  MAIN_GT1;
&nbsp;&nbsp;&nbsp;&nbsp;   }

&nbsp;&nbsp;&nbsp;&nbsp;   if(0 != fseek(stream, 0, SEEK_CUR))
&nbsp;&nbsp;&nbsp;&nbsp;   {
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;   printf( "Error! fseek != 0 \n" );
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;   goto  MAIN_GT1;
&nbsp;&nbsp;&nbsp;&nbsp;   }
&nbsp;&nbsp;&nbsp;}
&nbsp;&nbsp;&nbsp;
MAIN_GT1:
&nbsp;&nbsp;&nbsp;fclose( stream );
}


[ 本帖最后由 tgbvc 于 2008-2-20 16:49 编辑 ]

论坛徽章:
0
2 [报告]
发表于 2008-02-20 16:51 |只看该作者
TNND,害老子好苦!还指望CU能帮一下,嗨,令人失望。。。。。。。。。。。。。

[ 本帖最后由 tgbvc 于 2008-2-20 16:53 编辑 ]

论坛徽章:
0
3 [报告]
发表于 2008-02-20 16:56 |只看该作者
申请把此帖加精,

论坛徽章:
0
4 [报告]
发表于 2008-02-20 16:57 |只看该作者
哪里的BUG,说清楚,别人看要花时间的

论坛徽章:
0
5 [报告]
发表于 2008-02-20 17:00 |只看该作者
原帖由 思一克 于 2008-2-20 16:57 发表
哪里的BUG,说清楚,别人看要花时间的


     if(0 != fseek(stream, -1, SEEK_CUR))
       {
           printf( "Error! fseek != 0 \n" );
           goto  MAIN_GT1;
       }
     
      
       ch++;
       if(1 != fwrite(&ch, 1, 1, stream))
       {
           printf( "Error! fwrite != 0 \n" );
           goto  MAIN_GT1;
       }

       if(0 != fseek(stream, 0, SEEK_CUR))  // 这样文件指针才能复位,
       {
           printf( "Error! fseek != 0 \n" );
           goto  MAIN_GT1;
       }

论坛徽章:
0
6 [报告]
发表于 2008-02-20 17:08 |只看该作者
在fread和fwrite之间需要fseek(stream,0,SEEK_SET);

论坛徽章:
0
7 [报告]
发表于 2008-02-20 17:10 |只看该作者
原帖由 flw2 于 2008-2-20 17:08 发表
在fread和fwrite之间需要fseek(stream,0,SEEK_SET);


那不又文件头去了,怎么能顺序读,你还不明白题意吧。

论坛徽章:
0
8 [报告]
发表于 2008-02-20 17:16 |只看该作者
原帖由 tgbvc 于 2008-2-20 17:10 发表


那不又文件头去了,怎么能顺序读,你还不明白题意吧。

man fseek

论坛徽章:
0
9 [报告]
发表于 2008-02-20 17:17 |只看该作者
sorry是SEEK_CUR

论坛徽章:
0
10 [报告]
发表于 2008-02-20 17:20 |只看该作者
循环之所以不停,是因为read write的缓冲可能是不同的,或者文件当前位置有两个,一个读写

能否把FILE的定义找出来?
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP