求助:二进制文件读取出错
已经实现了从数据库向文本的写入
但反过来从文本读取存入数据库时,从中间某一行的某个字符开始读取信息不对,数目变得很大或很小,但不是随机的,因为每次都是同样的位置出错。数据也是一样的。
struct line_struct
{
int no;
int ibusid;
char ibusname[16];
int jbusid;
char jbusname[16];
float p;
float q;
float r;
float x;
int acdc;
bool draw;
char flow[16];
int flow_x;
int flow_y;
float angle;
int i_x;
int i_y;
int dir;
int j_x;
int j_y;
};
////////////// 在保存按钮里 读LINE表中的数据,并写入文件 /////////
tblLine-> Open();
tblLine-> First();
while(!tblLine-> Eof)
{
strLine.no=tblLine-> FieldByName( "NO ")-> Value;
strLine.ibusid=tblLine-> FieldByName( "IBUSID ")-> Value;
strcpy(strLine.ibusname,VarToStr(tblLine-> FieldByName( "IBUSNAME ")-> Value).c_str());
strLine.jbusid=tblLine-> FieldByName( "JBUSID ")-> Value;
strcpy(strLine.jbusname,VarToStr(tblLine-> FieldByName( "JBUSNAME ")-> Value).c_str());
strLine.p=tblLine-> FieldByName( "P ")-> Value;
strLine.q=tblLine-> FieldByName( "Q ")-> Value;
strLine.r=tblLine-> FieldByName( "R ")-> Value;
strLine.x=tblLine-> FieldByName( "X ")-> Value;
strLine.draw=tblLine-> FieldByName( "DRAW ")-> Value;
strcpy(strLine.flow,VarToStr(tblLine-> FieldByName( "FLOW ")-> Value).c_str());
strLine.flow_x=tblLine-> FieldByName( "FLOW_X ")-> Value;
strLine.flow_y=tblLine-> FieldByName( "FLOW_Y ")-> Value;
strLine.angle=tblLine-> FieldByName( "ANGLE ")-> Value;
strLine.i_x=tblLine-> FieldByName( "I_X ")-> Value;
strLine.i_y=tblLine-> FieldByName( "I_Y ")-> Value;
strLine.j_y=tblLine-> FieldByName( "J_Y ")-> Value;
strLine.i_x=tblLine-> FieldByName( "I_X ")-> Value;
strLine.dir=tblLine-> FieldByName( "DIR ")-> Value;
strLine.acdc=tblLine-> FieldByName( "ACDC ")-> Value;
fseek(savefile1,sizeof(strLine)*total,SEEK_SET);
fwrite(&strLine,sizeof(strLine),1,savefile1);
total++;
tblLine-> Next();
}
////////////////在打开按钮里,把文件读入线路表 /////////////////////
total++;
fseek(savefile1,sizeof(strLine)*total,SEEK_SET);
fread(&strLine,sizeof(strLine),1,savefile1);
tblLine-> Open();
tblLine-> First();
while(strLine.no!=-1)
{
tblLine-> Insert();
tblLine-> FieldByName( "NO ")-> Value=strLine.no;
tblLine-> FieldByName( "IBUSID ")-> Value=strLine.ibusid;
tblLine-> FieldByName( "IBUSNAME ")-> Value=strLine.ibusname;
tblLine-> FieldByName( "JBUSID ")-> Value=strLine.jbusid;
tblLine-> FieldByName( "JBUSNAME ")-> Value=strLine.jbusname;
tblLine-> FieldByName( "P ")-> Value=strLine.p; //////////////读到第九行的时这里的P读出来就是-8E(-23),而前面保存第九行是对的。
tblLine-> FieldByName( "Q ")-> Value=strLine.q;
tblLine-> FieldByName( "R ")-> Value=strLine.r;
tblLine-> FieldByName( "X ")-> Value=strLine.x;
tblLine-> FieldByName( "DRAW ")-> Value=strLine.draw;
tblLine-> FieldByName( "FLOW ")-> Value=strLine.flow;
tblLine-> FieldByName( "FLOW_X ")-> Value=strLine.flow_x;
tblLine-> FieldByName( "FLOW_Y ")-> Value=strLine.flow_y;
tblLine-> FieldByName( "ANGLE ")-> Value=strLine.angle;
tblLine-> FieldByName( "I_X ")-> Value=strLine.i_x;
tblLine-> FieldByName( "I_Y ")-> Value=strLine.i_y;
tblLine-> FieldByName( "J_Y ")-> Value=strLine.j_y;
tblLine-> FieldByName( "J_X ")-> Value=strLine.j_x;
tblLine-> FieldByName( "DIR ")-> Value=strLine.dir;
tblLine-> FieldByName( "ACDC ")-> Value=strLine.acdc;
tblLine-> Post();
total++;
fseek(savefile1,sizeof(strLine)*total,SEEK_SET);
fread(&strLine,sizeof(strLine),1,savefile1);
}
[解决办法]
当然有问题,在windows平台,对二进制文件,写入要用 "w+b ",读取要用 "r+b ",否则数据可能不正确