代码比较简单,请注意代码中标注的 “//出现问题处”将26改为其它值程序显示正常,
#include "stdafx.h"
#define Path "Config.sys"struct Config
{
unsigned long id; //机器标识
};int main(int argc, char* argv[])
{
struct Config cfgwrite;
struct Config cfgread;
int numwritten;
FILE *stream; /*写入配置信息*/
cfgwrite.id=26; //出现问题处
stream = fopen( Path, "wb" );
numwritten= fwrite ( &cfgwrite, sizeof( cfgwrite ), 1, stream);
fclose(stream); /*读取配置信息*/
stream = fopen( Path, "r" );
numwritten= fread ( &cfgread, sizeof( cfgread ), 1, stream);
fclose(stream); /*显示配置信息*/
printf("id is %u",cfgread.id); return 0;
}
#include "stdafx.h"
#define Path "Config.sys"struct Config
{
unsigned long id; //机器标识
};int main(int argc, char* argv[])
{
struct Config cfgwrite;
struct Config cfgread;
int numwritten;
FILE *stream; /*写入配置信息*/
cfgwrite.id=26; //出现问题处
stream = fopen( Path, "wb" );
numwritten= fwrite ( &cfgwrite, sizeof( cfgwrite ), 1, stream);
fclose(stream); /*读取配置信息*/
stream = fopen( Path, "r" );
numwritten= fread ( &cfgread, sizeof( cfgread ), 1, stream);
fclose(stream); /*显示配置信息*/
printf("id is %u",cfgread.id); return 0;
}
int main(int argc, char* argv[])
{
Config cfgwrite;
Config cfgread;
int numwritten;
FILE *stream; /*写入配置信息*/
cfgwrite.id=26; //出现问题处
stream = fopen( Path, "wb" );
numwritten= fwrite ( &cfgwrite, sizeof( cfgwrite ), 1, stream);
fclose(stream); /*读取配置信息*/
stream = fopen( Path, "r" );
numwritten= fread ( &cfgread, sizeof( cfgread ), 1, stream);
fclose(stream); /*显示配置信息*/
printf("id is %u",cfgread.id); return 0;
}
/*读取配置信息*/
stream = fopen( Path, "r" );
numwritten= fread ( &cfgread, sizeof( cfgread ), 1, stream);
fclose(stream);fopen的摸式改成"rb"看看。