reading binary file problem

The problem is that it is reading the records more than what is written and keeps repeating them again and again..please tell me the problem ... i am unable to identify it..


struct configStruct
{
char PointId[20];
char std_type[20];
char collectionId[20];
char arrayId[20];
}conf[10];

struct configStructRes
{
char PointId[20];
char std_type[20];
char collectionId[20];
char arrayId[20];
}confres[10];



strcpy(conf[0].PointId,"Ac_En_02");strcpy(conf[0].std_type,"HST_1MIN");strcpy(conf[0].collectionId,"C1");strcpy(conf[0].arrayId,"R1");
strcpy(conf[1].PointId,"Ac_En_03");strcpy(conf[1].std_type,"HST_1MIN");strcpy(conf[1].collectionId,"C2");strcpy(conf[1].arrayId,"R2");
strcpy(conf[2].PointId,"Ac_En_04");strcpy(conf[2].std_type,"HST_6MIN");strcpy(conf[2].collectionId,"C3");strcpy(conf[2].arrayId,"R3");
strcpy(conf[3].PointId,"Ac_En_05");strcpy(conf[3].std_type,"HST_6MIN");strcpy(conf[3].collectionId,"C4");strcpy(conf[3].arrayId,"R4");
strcpy(conf[4].PointId,"Ac_En_06");strcpy(conf[4].std_type,"HST_1HOUR");strcpy(conf[4].collectionId,"C5");strcpy(conf[4].arrayId,"R5");
strcpy(conf[5].PointId,"Ac_En_07");strcpy(conf[5].std_type,"HST_1HOUR");strcpy(conf[5].collectionId,"C6");strcpy(conf[5].arrayId,"R6");




/* Writing the Structure to the Binary File*/
for(int i=0;i<6;i++)
{
stream.write((char*)(&conf[i]),sizeof(configStruct));
cout<<conf[i].PointId<<conf[i].std_type<<endl;
//cout<<i<<endl;
}

stream.seekg(0); /* Moving the file pointer to the start.*/
/* Reading the structure from the file */
i =0;

cout<<"---Reading the file---"<<endl;

stream.read((char*)(&confres[i]),sizeof(configStructRes));
while(!stream.eof())
{
cout<<confres[i].PointId<<confres[i].std_type<<confres[i].collectionId<<confres[i].arrayId<<endl;
i++;
stream.read((char*)(&confres[i]),sizeof(configStructRes));

}
Last edited on
Shouldn't
stream.read((char*)(&confres),sizeof(configStructRes));
be
stream.read((char*)(&confres[i]),sizeof(configStructRes));?
I think I solved it myself after lot of brain crunching...


the problem was ... i didnt close the stream after writing into it.
so there was no "EOF" character in the file..that is why it was going on and on..
Moreover this memory was corrupting the global variables also...which actually forced me to find a solution to the problem . since it didnt cause any program crash.

the structure into which i was reading the file was getting larger and larger and was overwriting the nearby memory locations due to the buffer over run.
Use
while(stream)
rather than
while(!stream.eof())
Topic archived. No new replies allowed.