Incorrect data input with fstream
I tried to read in data from a text file using fstream but got wrong data.
ifstream fin ("C:\\Users\\rEgonicS\\Documents\\test.in");
int number;
fin >> number;
cout << number;
开发者_运维问答test.in
is simply 12
.
cout
reads 4273190
.
Can someone explain why this is so and how to fix it?The most likely cause is that the file open failed. Check the status after opening, and also after reading; for a simple test, do something like this:
ifstream fin ("C:\\Users\\rEgonicS\\Documents\\test.in");
if (!fin) cout << "File open failed\n";
int number;
fin >> number;
if (!fin) cout << "File read failed\n";
cout << number;
This might give a further clue as to what's going on.
精彩评论