You're opening the file in binary mode. The extraction operators were meant to be used with text files. Simply leave out the std::ios::binary
flag to open the file in text mode.
If you actually do have a binary file, use the read()
function instead.
Edit: I tested it too, and indeed it seems to work. I got this from CPlusPlus.com, where it says:
In binary files, to input and output data with the extraction and insertion operators (<< and >>) and functions like getline is not efficient, since we do not need to format any data, and data may not use the separation codes used by text files to separate elements (like space, newline, etc...).
Together with the description of ios::binary
, which simply states "Consider stream as binary rather than text.", I'm utterly confused now. This answer is turning into a question of its own...