C++ 如何在C+;中的文件末尾添加数据+;?

C++ 如何在C+;中的文件末尾添加数据+;?,c++,ofstream,C++,Ofstream,我已经按照网络上的说明进行了操作,假设这段代码将输入添加到文件“database”的末尾。但当我检查时,数据会凌驾于现有数据之上。请帮忙,这是我的代码: int main(){ string name; string address; string handphone; cout << "Name: "; getline(cin, name); cout << "Address: "; getline(cin, ad

我已经按照网络上的说明进行了操作,假设这段代码将输入添加到文件“database”的末尾。但当我检查时,数据会凌驾于现有数据之上。请帮忙,这是我的代码:

int main(){
    string name;
    string address;
    string handphone;
    cout << "Name: ";
    getline(cin, name);
    cout << "Address: ";
    getline(cin, address);
    cout << "Handphone Number: ";
    getline(cin, handphone);
    ofstream myfile("database", ios_base::ate);
    myfile.seekp( 0, ios_base::end );
    myfile << name<<endl;
    myfile << address<<endl;
    myfile << handphone<<endl;
    myfile.close();
}
intmain(){
字符串名;
字符串地址;
字符串手机;
不能使用:

ofstream myfile("database",  ios::out | ios::app);
ios::out
:为输出操作打开


ios::app
:所有输出操作都在文件末尾执行,将内容附加到文件的当前内容中。此标志只能用于仅为输出操作打开的流。

尝试将
out
添加到流打开模式…如[C++of stream Overwriting][1]中所述,您需要在流标志中使用ios::app。[1]: