|
|
今日在SDK下,定义了一结构: MAX=63
-------------h文件中-------------
struct object //部件
{
public:
char objname[20]; //物体名称
int code; //代码
char mdlfile[20]; //mdl文件路径
char imgfile[20]; //图片文件路径(地图编辑器使用)
int move_type; //初运动类型
int move_speed; //部件运动速度
int after_type; //中弹后运动类型,0为不响应中弹。
};
struct object objs[MAX];
然后在CPP中将个结构赋值后进行读写:
----Cpp文件中--------
void CObjectEditDlg::set_value()
for(int i=0;i<MAX;i++)
{
sprintf(objs.objname,"newobj%d",i+1);
objs.code=i+1;
sprintf(objs.imgfile,"yy.img");
sprintf(objs.mdlfile,"xx.mdl");
objs.move_type=rand()%100;
objs.move_speed=rand()%100;
objs.after_type=rand()%100;
}
void CObjectEditDlg::save_bin() //从数组中写进二进制文件中
{
ofstream out("object.dat",ios: ut|ios::app|ios::trunc);
for(int i=0;i<MAX;i++)
{
out.write((unsigned char *)&objs,sizeof(struct object));
if(!out)
MessageBox("写文件失败");
}
if(out)
MessageBox("已经将修改后的各部件属性写入object.dat文件");
out.close();
}
void CObjectEditDlg::load_bin() //从二进制文件中读到数组中
{
ifstream in("object.dat");
int i=0;
while(!in.eof())
{
in.read((unsigned char *)&objs,sizeof(struct object));
i=i+1;
}
in.close();
}
结果写入的文件中,用记事本打开,发现有newobj63等字样,但读出文件,前20个结构是正确的,后面的全部变成乱码,希望有人指点一二,读写的dat文件付后:
|
|