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:
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.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();
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:
The output of the program at about the tenth buffer read is: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();
"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?




Reply With Quote