有关fwrite()和fread()函数使用讨论,参与者皆可得分

Awang_126 2003-09-27 06:06:12
下面是一段小程序,主要是把一个unsigned int型数据用fwrite()保存到一个文件中,然后用fread()读出来,但是发现读出来的和我写进去的数不相同,请问是什么原因?是在VC6.0下写的程序.
#include "stdafx.h"
#include <iostream.h>

//***********//
void main( void )
{
FILE *stream;
unsigned int fields_count=0x677524;
unsigned short temp;
if( (stream = fopen( "test", "w+b" )) != NULL )
{
fwrite(&fields_count,sizeof(unsigned int),1,stream);
printf("write=%x\n",fields_count);
fread(&fields_count,sizeof(unsigned int),1,stream);
printf("read=%x\n",fields_count);
}
fclose(stream);
}
程序执行后,显示为:

write=677524
read=cdcdcdcd

请问是什么原因,希望各位指点一下,


...全文
64 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
dingo2000 2003-10-03
  • 打赏
  • 举报
回复
移动指针啊。。。
lemon520 2003-10-03
  • 打赏
  • 举报
回复
我是李逍遥!
嘿嘿!
Awang_126 2003-10-03
  • 打赏
  • 举报
回复
谢谢各位的参与:)
金来 2003-09-28
  • 打赏
  • 举报
回复
失败,以前也有过类似的,不过我都是先fclose后在fopen一次。。。。。。
pcyy 2003-09-28
  • 打赏
  • 举报
回复
^_^
ok
cnxiaohai 2003-09-28
  • 打赏
  • 举报
回复
小虎呀

我是七七

哈哈

看来是个仙剑的通道中人

#include <stdio.h>
void main( void )
{
FILE *stream;
unsigned int fields_count=0x677524;
unsigned short temp;
if( (stream = fopen( "c:\\test", "r+" )) != NULL )
{
fwrite(&fields_count,sizeof(unsigned int),1,stream);
printf("write=%x\n",fields_count);
fseek(stream, 0, SEEK_SET);//把指针移回去
fread(&fields_count,sizeof(unsigned int),1,stream);
printf("read=%x\n",fields_count);
}
fclose(stream);
}

同时移动读写指针

是文件操作时候的关键
doer_ljy 2003-09-28
  • 打赏
  • 举报
回复
读些模式,和移动文件指针!
对于流文件读写,最好还是不要同时进行吧?
因为不知道是不是会有缓冲的问题。
tolixiaohui 2003-09-27
  • 打赏
  • 举报
回复
ok
sharkhuang 2003-09-27
  • 打赏
  • 举报
回复
OK!永远不死机!好名字啊!
playboyxp 2003-09-27
  • 打赏
  • 举报
回复
调试通过
#include <stdio.h>
void main( void )
{
FILE *stream;
unsigned int fields_count=0x677524;
unsigned short temp;
if( (stream = fopen( "c:\\test", "r+" )) != NULL )
{
fwrite(&fields_count,sizeof(unsigned int),1,stream);
printf("write=%x\n",fields_count);
fseek(stream, 0, SEEK_SET);//把指针移回去
fread(&fields_count,sizeof(unsigned int),1,stream);
printf("read=%x\n",fields_count);
}
fclose(stream);
}
nirvana_ya 2003-09-27
  • 打赏
  • 举报
回复
同时移动读写指针
nirvana_ya 2003-09-27
  • 打赏
  • 举报
回复
这是读写方式的原因,你具体指明二进制试试看
zxm521b 2003-09-27
  • 打赏
  • 举报
回复
在fread前加上fseek(stream, 0, SEEK_SET);

69,371

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧