-
August 6th, 2010, 12:44 PM
#1
Debugging ifstream read errors
I am not used to working with the std c++ file stream objects and am having trouble understanding how to debug this error condition. I am reading a binary file that consists of a series of pairs of 16-bit sizes followed by a block of data of that size. For example:
Code:
ifstream f("x.bin");
while(!f.fail())
{
// Read 16-bit size
unsigned short nBytes;
f.read((char *)&nBytes, sizeof(nBytes));
// Allocate a buffer of size nBytes
char *pBytes = new char[nBytes];
// Read nBytes
f.read(pBytes, nBytes);
// Delete buffer
delete [] pBytes
}
f.close();
This is just a basic reduction of my actual code to something that can be understood at a quick glance. The difference in my real code is that I analyze the buffer of data that is read and store it in an appropriate place in my program.
There are about 100 pairs of sizes and data buffers in the file and everything works as expected when reading approximately 10 pairs, then the reading gets out of synch with the data. I started using tellg() to mark the file position before and after the buffer read to compare with the number of bytes to be read and have found that at the point of error they do not agree. For example:
Code:
ifstream f("x.bin");
while(!f.fail())
{
// Read 16-bit size
unsigned short nBytes;
f.read((char *)&nBytes, sizeof(nBytes));
// Allocate a buffer of size nBytes
char *pBytes = new char[nBytes];
// Mark the file position before the buffer read
long p1 = f.tellg();
// Read nBytes
f.read(pBytes, nBytes);
// Mark the file position after the buffer read
long p2 = f.tellg();
// Compare file position change with number of bytes that were to have been read
if((p2-p1)!=nBytes)
cout << "Error: number of bytes = " << nBytes << ", file position change = " << p2-p1 << endl;
// Delete buffer
delete [] pBytes
}
f.close();
The output of the program at about the tenth buffer read is:
"Error: number of bytes = 76, file position change = 81"
In short, I try to read 76 bytes from the buffer but the file position changes by 81 bytes. What could possibly account for this and how can I detect it?
Last edited by 0xC0000005; August 6th, 2010 at 12:47 PM.
-
August 6th, 2010, 01:11 PM
#2
Re: Debugging ifstream read errors
If you're on Windows, or you're working with files that were generated on Windows, then you could be hitting end-of-line conversion problems. I suggest opening the file in binary mode:
Code:
ifstream f("x.bin", ifstream::binary);
Do this for both reading and writing.
-
August 6th, 2010, 01:42 PM
#3
Re: Debugging ifstream read errors
-
August 6th, 2010, 01:44 PM
#4
Re: Debugging ifstream read errors
As luck would have it, I just solved the same issue a few hours ago, so it was fresh in my mind.
-
August 6th, 2010, 02:20 PM
#5
Re: Debugging ifstream read errors
I should have known - I am used to working mainly with MFC's CFile which behaves in a similar a manner depending on the flags. You must have a lot of "I approve" ratings from me as I'm prohibited from giving you another one at this time.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|