Are you sure that you are reading the data file correctly?
I see several possible issues.
1 2 3 4 5 6 7 8 9
|
while(!myReadFile.eof()){
myReadFile.read((char *)(&logger), sizeof(logger));
if(logger == 'A'){
myReadFile.read((char *)(&temp), sizeof(temp));
cout << temp << ", " ;
myReadFile.read((char *)(&locTime), sizeof(locTime));
cout << locTime << endl;
}
}
|
First, the eof() test not a good idea. Your code should perform a read operation, and only proceed to do something with the result if the read was successful. At the very least, the eof() test is too late, as it always testing what happened last time, but cannot predict what will happen within the body of the loop when the next read is attempted.
Another issue is that the
logger
value is read from the file, and only if it contains the letter 'A' is the remainder of the data read. That means as soon as a non-'A' record is encountered, the reading will get out of synchronisation. I believe you should
always read all of the items of that record, in order that the next time around, the file position will be at the start of the next record.
And finally there is the issue of padding and alignment. I don't know how the data was originally written to the data file. If the entire structure
DataRecord
was written as a single block of bytes, there may be some padding, in order to ensure that the numeric items are located at the correct memory boundary, say a multiple of 2, 4 or 8 bytes depending on type of item.
When I tried to create a sample file on my system, each data record occupied anywhere from 13 to 24 bytes, depending upon this padding.