参考网址:

(128条消息) C++ 深究fstream打开方式ios::app,ios::ate,ios::in,ios::out_悟名堂的博客-CSDN博客_fstream ios::app

例程

默认(覆盖原文件)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include<fstream>
#include<iostream>
using namespace std;
int main()
{
char data[100];

//第一次打开文件
ofstream outfile;
outfile.open("D:\\afile.dat");
cout << "Writing to the file" << endl;
cout << "Enter your name:";
cin.getline(data, 100);
outfile << data << endl;
outfile.close();

//第二次打开文件
ofstream outfile2;
outfile2.open("D:\\afile.dat");//**待修改语句**
cout << "Enter your age:";
cin >> data;
outfile2 << data << endl;
outfile2.close();//第二次关闭文件

return 0;
}

使用iso::app(在文末尾添加)

1
outfile2.open("D:\\afile.dat", ios::app);