Any idea why the following would fail?
std::fstream i(L"C:/testlog.txt", std::ios::binary | std::ios::in); int test = 0; i >> test;
fail() is returning true. The file exists and is opened.
I checked
i._Filebuffer._Myfile._ptr
and it is pointer to a buffer of the file so I don’t see why it is failing.
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::binaryflag 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:
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…