主题:C++中的I\O流问题求助
#include <iostream>
#include <fstream>
#include <cstdlib>
using namespace std;
int main()
{double aver,bzc,m,sum=0;
int count=0;
ifstream fin;
fin.open("shuzi.dat");
if (fin.fail())
{ cout<<"Input file opening failed.\n";
exit(1);
}
while(fin>>m)
{if(m!=' ')
{sum=sum+m;
count++;
}
}
fin.close();
aver=sum/count;
cout.setf(ios::fixed);
cout.setf(ios::showpoint);
cout.precision(3);
cout<<"the average is:"<<aver<<endl;
fin.open("shuzi.dat");
if (fin.fail())
{ cout<<"Input file opening failed.\n";
exit(1);
}
while(fin>>m)
{if(m!=' ')
{bzc=(m-aver)*(m-aver);
cout<<"the bzc is"<<bzc<<endl;
}
}
fin.close();
return 0;
}
我写的这个程序是想把shuzi.dat文件中的双精度数字求平均,再分别求标准差,为啥平均求的事对的,可求不了标准差,运行显示Input file opening failed???
#include <fstream>
#include <cstdlib>
using namespace std;
int main()
{double aver,bzc,m,sum=0;
int count=0;
ifstream fin;
fin.open("shuzi.dat");
if (fin.fail())
{ cout<<"Input file opening failed.\n";
exit(1);
}
while(fin>>m)
{if(m!=' ')
{sum=sum+m;
count++;
}
}
fin.close();
aver=sum/count;
cout.setf(ios::fixed);
cout.setf(ios::showpoint);
cout.precision(3);
cout<<"the average is:"<<aver<<endl;
fin.open("shuzi.dat");
if (fin.fail())
{ cout<<"Input file opening failed.\n";
exit(1);
}
while(fin>>m)
{if(m!=' ')
{bzc=(m-aver)*(m-aver);
cout<<"the bzc is"<<bzc<<endl;
}
}
fin.close();
return 0;
}
我写的这个程序是想把shuzi.dat文件中的双精度数字求平均,再分别求标准差,为啥平均求的事对的,可求不了标准差,运行显示Input file opening failed???