关于 ofstream ios::app 的问题
#include <iostream>#include <fstream>
using namespace std;
void savefile()
{
int a[10],b[10];
ofstream outfile1("f1.dat"),outfile2("f2.dat");
int j=1;
for (int i=0;i<10;i++)
{
a[i]=j;
outfile1<<a[i]<<" ";
j++;
}
for (i=0;i<10;i++)
{
b[i]=j;
outfile2<<b[i]<<" ";
j++;
}
outfile1.close();
outfile2.close();
}
void getfile()
{
int a[10],b[10];
ifstream infile1("f1.dat"),infile2("f2.dat");
for (int i=0;i<10;i++)
{
infile1>>a[i];
cout<<a[i]<<" ";
}
cout<<endl;
for (i=0;i<10;i++)
{
infile2>>b[i];
cout<<b[i]<<" ";
}
cout<<endl;
infile1.close();
infile2.close();
}
void addfile()
{
ifstream infile("f1.dat");
ofstream outfile("f2.dat",ios::app);
int a[10];
for (int i=0;i<10;i++)
{
infile>>a[i];
outfile<<a[i]<<" ";
cout<<a[i];
}
infile.close();
outfile.close();
}
int main()
{
savefile();
addfile();
getfile();
return 0;
}
运行后,不能在 f2.dat 文件后面加入数据,请大家指教一下