// a8.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include <iostream>
#include <fstream>
#include <cstdlib>
using namespace std;
const int SIZE=60;
int _tmain(int argc, _TCHAR* argv[])
{
char filename[SIZE];
ifstream infile;
cout<<"Enter name of data file:";
cin.getline(filename,SIZE);
infile.open(filename);
if(!infile.is_open())
{
cout<<"Cound not open the file :"<<filename<<endl;
cout<<"Program terminating .\n"<<endl;
exit(EXIT_FAILURE);
}
double value;
double sum=0.0;
int count=0;
infile>>value;
while(infile.good())
{
++count;
sum+=value;
infile>>value;
}
if(infile.eof())
{
cout<<"End of file reached;\n";
}
else if(infile.fail())
{
cout<<"Input terminated by data mismatch.\n";
}
else
{
cout<<"Input terminated for unknown reason.\n";
}
if(count==0)
cout<<"No data processed.\n";
else
{
cout<<"Item read:"<<count<<endl;
cout<<"Sum:"<<sum<<endl;
cout<<"Average:"<<double(sum/count)<<endl;
}
infile.close();
return 0;
}
2017.06.04 c++文件学习
最新推荐文章于 2023-06-18 10:27:41 发布