Hi all,
Does anyone here know of a way a C++ ifstream's get pointer might get corrupted after a read() call? I'm seeing some truly bizarre behaviour that I'm at a loss to explain. For example (illustrative code, rather than what I'm actually running):
int main()
{
// datafile.bin is a 2MB binary file...
std::ifstream ifs( "datafile.bin", ios::binary );
ifs.exceptions ( ifstream::eofbit | ifstream::failbit | ifstream::badbit );
int data[100];
std::istream::pos_type current_pos = ifs.tellg();
// current_pos = 0, as you'd expect...
ifs.read( reinterpret_cast<char*>(data), 100 * sizeof(int) );
// throws no exception, so no error bits set...
std::streamsize bytes_read = ifs.gcount();
// gives 400, as you'd expect...
current_pos = ifs.tellg();
// current_pos = 0x1e1a or something similarly daft
return 0;
}
My example shows an array read, but it's happened even when reading single values of built-in types; the get pointer before the read is correct, the gcount() call reports the correct number of bytes read, but afterwards the get pointer is completely screwy. This doesn't happen with every read() call - sometimes I get through bunches of them before one stuffs up. What could possibly be monkeying with the get pointer? Am I doing something profoundly stupid?
Any and all help greatly appreciated...
Simon