Spurious end of file error from ifstream::read
-
I'm using ofstream and ifstream for serialization purposes, using this function to read values from the file back into variables:
template
T read(std::ifstream&savedGame)
{
char buffer[sizeof(T)];
savedGame.read(buffer, sizeof(T));
return *(T*)&buffer;
}This is the file I'm reading out of: test.chess - Google Drive[^] The deserialization works until the point that I try to do a 4-byte read whose first value corresponds to the N with the tilde a little less than halfway through that file, as viewed in notepad. At that point, the watch window tells me the first three entries of buffer get filled, matching the file, with a tilda N, an ellipsis, and a trademark symbol (hexidecimal values D1, 85, and 99), while the fourth entry is left uninitialized. When I call savedGame.rdstate(), I get a value of 3, which means eofbit and failbit are set: ios_base::iostate - C++ Reference[^]. All subsequent reads leave the whole buffer uninitialized. I can see that the file goes on after that - the character that should have gone in the fourth buffer entry has hexidecimal value 1A - so what could the problem be? I can post more of the surrounding code if that would be relevant.
-
I'm using ofstream and ifstream for serialization purposes, using this function to read values from the file back into variables:
template
T read(std::ifstream&savedGame)
{
char buffer[sizeof(T)];
savedGame.read(buffer, sizeof(T));
return *(T*)&buffer;
}This is the file I'm reading out of: test.chess - Google Drive[^] The deserialization works until the point that I try to do a 4-byte read whose first value corresponds to the N with the tilde a little less than halfway through that file, as viewed in notepad. At that point, the watch window tells me the first three entries of buffer get filled, matching the file, with a tilda N, an ellipsis, and a trademark symbol (hexidecimal values D1, 85, and 99), while the fourth entry is left uninitialized. When I call savedGame.rdstate(), I get a value of 3, which means eofbit and failbit are set: ios_base::iostate - C++ Reference[^]. All subsequent reads leave the whole buffer uninitialized. I can see that the file goes on after that - the character that should have gone in the fourth buffer entry has hexidecimal value 1A - so what could the problem be? I can post more of the surrounding code if that would be relevant.