eof() changing behaviour?

I'm using ifstream to read data from a file, with eof() to see when I'm at the end of it (and in the example below writing double data to screen). I've been using it in the following way for a while:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
int main() {

	ifstream infile;
	infile.open("file.dat");
	double d;
	infile >> d;
	while(!infile.eof()) {
		cout << d << endl;
		infile >> d;
	}
	infile.close();
	
	return 0;
}


And it has worked perfectly. Now however, it suddenly decides to ignore the last data in the file. If I change it to

1
2
3
4
5
6
7
8
9
10
11
12
13
int main() {

	ifstream infile;
	infile.open("file.dat");
	double d;
	while(!infile.eof()) {
		infile >> d;
		cout << d << endl;
	}
	infile.close();
	
	return 0;
}


it works fine, but I thought this was supposed to be wrong (reading the last data twice). I'm uncomfortable with switching to the last method in case it suddenly works the normal way again... ideas?
That will be dependant if your file ends with a line break.
Use this instead while( infile>>d )
And it works, thanks!
Topic archived. No new replies allowed.