为什么第2次打开文件会出错??
#include<iostream>
#include<fstream>
#include<cstdlib>
#include<iomanip>
using namespace std;
int main()
{
double pingjun(ifstream& in_stream,ofstream& out_stream);
void pfg(ifstream& in_stream,ofstream& out_stream,double pa);
double pj;
ifstream fin;
ofstream fout;
fin.open("d.dat");
if(fin.fail())
{
cout<<"input file opening failed"<<endl;
exit(1);
}
fout.open("d1.dat");
if(fout.fail())
{
cout<<"output file opening failed"<<endl;
}
pj=pingjun(fin,fout);
cout.setf(ios::fixed);
cout.setf(ios::showpoint);
cout.setf(ios::showpos);
cout.precision(2);
cout<<"这些数字的平均数是"<<pj<<endl;
fin.close();
fin.open("d.dat");
if(fin.fail())
{
cout<<"input file opening failed"<<endl;
exit(1);
}
pfg(fin,fout,pj);
fin.close();
fout.close();
return 0;
}
double pingjun(ifstream& in_stream,ofstream& out_stream)
{
double next,sum=0;
int n=0;
while(in_stream>>next)
{
cout<<next<<endl;
sum=sum+next;
n++;
}
cout<<n<<" "<<sum<<endl;
double p_j;
p_j=(sum/n);
out_stream<<"平均数是"<<p_j<<endl;
return p_j;
}
这是源码
算平均数能算对,但是第2次打开D.DAT时却报错,求助高手!