文件读入问题
这是我学习文件读入时写的一个程序,请大家帮个忙,为什么总是少读一个数字呢?多谢指点.#include <iostream>
#include <fstream>
#include <cstdlib>
using namespace std;
const int SIZE = 60;
int main()
{
char fileName[SIZE];
ifstream inFile;
cout << "Please enter the file name:";
cin.getline(fileName,SIZE);
inFile.open(fileName);
if(!inFile.is_open())
{
cout << "Sorry! This file can't open ."
<< "Program terminating.";
exit(EXIT_FAILURE);
}
double value;
double sum=0;
int count=0;
inFile >> value;
while(inFile.good())
{
++count;
sum += value;
inFile >> value;
}
if(inFile.eof())
cout << "It's the end of the file.\n";
else if(inFile.fail())
cout << "Input terminated by data mismatch.\n";
else
cout << "Input terminated by unknown reason.\n";
if(0 == count)
cout << "No data processed.\n";
else
{
cout << "Items read:" << count << endl;
cout << "Sum:" << sum << endl;
cout << "Average:" << sum/count << endl;
}
inFile.close();
return 0;
}