If I do something like the following:
ifstream file;
file.open("somefile", ios::binary);
unsigned int data;
file >> data;
My stream will always set the failbit
and the data
will remain uninitialized. However, if I read a char
or unsigned char
instead, the stream is fine. perror()
is telling me "result too large".
The only thing I saw on Google was a suggestion saying that operator>>
shouldn't be used for binary data (prefer read()
), but I find the operator to be cleaner and easier to use -- and it doesn't require casting everything.
Can someone explain this issue?
See Question&Answers more detail:os