请问这个程序怎么不能从文件里读出数据....
#include "iostream.h"
#include "fstream.h"
#include "stdlib.h"
void get_file()
{
int a[20],b[10],i;
ofstream outfile1("f1.dat",ios::out);
if(!outfile1)
{
cerr<<"error"<<endl;
exit(1);
}
ifstream infile1("f1.dat",ios::in|ios::nocreate);
if(!infile1)
{
cerr<<"error"<<endl;
exit(1);
}
cout<<"enter 10 integer to f1"<<endl;
for(i=0;i<10;i++)
{
cin>>a[i];
outfile1<<a[i]<<" ";
}
for(i=0;i<10;i++)
{
infile1>>b[i];
cout<<b[i]<<" ";
}
cout<<endl;
outfile1.close();
infile1.close();
}
int main()
{
get_file();
return 0;
}
参考答案:outfile1.close()应该写在
for(i=0;i<10;i++)
{
cin>>a[i];
outfile1<<a[i]<<" ";
}
之后;
即:
for(i=0;i<10;i++)
{
cin>>a[i];
outfile1<<a[i]<<" ";
}
outfile1.close();
就可以了.