Hi
i created a text file and put some numbers to it
but when i read from it, the last number will be printed out twice
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main()
{
ofstream newfile("numbers.txt", ios::out);
if (!newfile){
cout << "File not found" << endl;
exit(1);
}
newfile << 54 << endl;
newfile << 12 << endl;
newfile << 45 << endl;
newfile.close();
int number;
string name;
cout << "Enter name of a file: ";
cin >> name;
ifstream openfile(name.c_str(), ios::in);
if (!openfile){
cout << "File not found!" << endl;
exit(1);
}
while(!openfile.eof()){
openfile >> number;
cout << number << endl;
}
openfile.close();
system("PAUSE");
return 0;
}