读取文本条件,如何和文件关联起来
// sumafile.cpp -- functions with an array argument#include <iostream>
#include <fstream>
#include <cstdlib>
const int SIZE = 60;
int main()
{
using namespace std;
char filename[SIZE];
ifstream inFile;
cout << "Enter name of data file: ";
cin.getline(filename, SIZE);
inFile.open(filename);
if (!inFile.is_open())
{
cout << "Could not open the file: " << filename << endl;
cout << "Program terminating.\n";
exit(EXIT_FAILURE);
}
double value;
double sum = 0.0;
int cout = 0;
inFile >> value;
while (inFile.good())
{
++count;
sum += value;
inFile >> value;
}
if (inFile.eof())
cout << "End of file reached.\n" << endl;
else if(inFile.fail())
cout << "Input terminated by data mismatch.\n" << endl;
else
cout << "Input terminated for unknow reason.\n" << endl;
if(count == 0)
cout << "No data processed.\n" << endl;
else
{
cout << "Items read: " << count << endl;
cout << "Sum: " << sum << endl;
cout << "Average: " << sum / count << endl;
}
inFile.close();
return 0;
}
首先要创建一个包含数字的文本文件,要不然就不能运行该文件,如何将文本文件和程序关联起来??向大侠们求助