I want to read the data from the file afile.dat and then print the data to the screen. The content of the file is as follows:
Max
20
In order to print the two items to the screen I wrote the following C++ program:
#include <fstream>
#include <iostream>
using namespace std;
int main ()
{
char data[100];
ifstream infile;
infile.open("afile.dat");
cout << "Reading from the file" << endl;
while(infile){
infile >> data;
cout << data << endl;
}
infile.close();
return 0;
}
The output of this program is:
Max
20
20
Why is "20" printed twice?