My program is supposed be reading a table that has two numbers: 4 byte integer to the start of the data within the file, and 4 byte integer of the data's size. The file looks like this (in hex) in the problem area:
I call the same function twice, void IO_Read32(std::ifstream &s, unsigned int &x);
Code:
IO_Read32(infile, data_off);
IO_Read32(infile, data_size);
Code:
void IO_Read32(std::ifstream &s, unsigned int &x)
{
unsigned char c;
int i;
x = 0;
for(i = 0; i < 4; ++i)
{
std::cout << "Now at " << s.tellg() << std::endl;
s >> c;
x |= c << (i * 8);
}
}
The cout line is for debugging, I had originally thought I had seek()'d one byte too far. This is not the case, as that line revealed. The program writes:
Code:
Now at 5171581
Now at 5171583
Now at 5171584
Now at 5171585
Now at 5171586
Now at 5171587
Now at 5171588
Now at 5171589
Notice the lack of byte 5171582! It doesn't read that byte, causing all of the following reads to be one byte off. Why?
The first byte is a 0x0D, so I was thinking newlines, but the file is opened for binary input. (It's not a text file.) I open the file:
Code:
infile.open(filename.c_str(), std::ios::binary | std::ios::in);
infile.is_open() returns true afterwards. Does this have something to do with the way I'm reading the file, and is there some better way to do single byte reads?