I have a question about the ifstream::operator>> behavior in the following code:
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
int main () {
ifstream inFile("test.txt");
string buffer;
while (!inFile.eof()) {
inFile >> buffer;
cout << buffer << endl;
}
return 0;
}
This code works perfectly well if the last line of test.txt is not empty, for instance :
One two
Three four
Five six
However if test.txt is written like that :
One two
Three four
Five six
(empty line)
The cout
will display two "six" strings.
Is it a problem related to the \r\n of Windows or something like that ?
I use Microsoft VC++ 2010.
Thanks in advance.