I am compiling the following code under Microsoft Visual Studio 2013
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
    ifstream in("filename.txt");
    int n;
    while (!in.eof()) {
        in >> n;
        cout << n << endl;
    }
    return 0;
}
filename.txt contains:
4
 
note: the second line is empty line.
I am getting this output:
4
4
My question is: Why 4 is duplicated in the output when I add trailing spaces or empty lines to the end of the file? and why eof is not working properly in this case? 
 
     
     
    