tags:

views:

43

answers:

1

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?

Thanks in advance.

*Stackoverflow thought I was a bot ; o ;.

+3  A: 

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.

Mike Seymour
I ran the above program and both "File open failed" and "File read failed" came up.
rEgonicS
So that means it failed to open (it will then fail to read since it's not open). Is the path correct? Is the file readable?
Mike Seymour