首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

fopen遇到奇怪的有关问题!

2012-04-19 
fopen遇到奇怪的问题!!!!!!!!!!!!!!我最近开始使用fopen,fread,fwrite。我为了保存游戏的进度写了这样一段

fopen遇到奇怪的问题!!!!!!!!!!!!!!
我最近开始使用fopen,fread,fwrite。
我为了保存游戏的进度写了这样一段代码:
fopen("user.txt", "w");
fopen("user.txt", "r");
然后使用fread,再然后读取了60几个字节之后就再不往后读了。
再再然后我换成了:
fopen("user.txt", "wb");
fopen("user.txt", "rb");
这样就没问题了,不知道什么原因。


[解决办法]
保存的内容是不是有结构体?
[解决办法]
二进制的吧
[解决办法]
msn例子不就是这样子的呀!应该可以的吧!

C/C++ code
/* FREAD.C: This program opens a file named FREAD.OUT and * writes 25 characters to the file. It then tries to open * FREAD.OUT and read in 25 characters. If the attempt succeeds, * the program displays the number of actual items read. */#include <stdio.h>void main( void ){   FILE *stream;   char list[30];   int  i, numread, numwritten;   /* Open file in text mode: */   if( (stream = fopen( "fread.out", "w+t" )) != NULL )   {      for ( i = 0; i < 25; i++ )         list[i] = (char)('z' - i);      /* Write 25 characters to stream */      numwritten = fwrite( list, sizeof( char ), 25, stream );      printf( "Wrote %d items\n", numwritten );      fclose( stream );   }   else      printf( "Problem opening the file\n" );   if( (stream = fopen( "fread.out", "r+t" )) != NULL )   {      /* Attempt to read in 25 characters */      numread = fread( list, sizeof( char ), 25, stream );      printf( "Number of items read = %d\n", numread );      printf( "Contents of buffer = %.25s\n", list );      fclose( stream );   }   else      printf( "File could not be opened\n" );}
[解决办法]
fopen("user.txt", "w");
fopen("user.txt", "r");
这两种文件打开模式默认为文本文件,那么fread 相对的fwrite操作时可能将某个相对于文本文件为结束符的编码(查查ASCII码表)写入了文件。
[解决办法]
很有可能是这个巧合:
popen句柄达到上限,你修改后重新运行自动是好的了。
如果是此原因可以用ace的fopen来解决。

热点排行