【问题标题】:How do I add data at the end of a file in C++?如何在 C++ 中的文件末尾添加数据?
【发布时间】:2012-12-24 06:13:22
【问题描述】:

我已按照网上的说明进行操作,此代码假定将输入添加到文件“数据库”的末尾。但是当我检查时,数据会覆盖现有数据。请帮忙,这是我的代码:

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();
}

【问题讨论】:

标签: c++ ofstream


【解决方案1】:

用途:

ofstream myfile("database",  ios::out | ios::app);

ios::out:为输出操作打开。

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-04-28
    • 1970-01-01
    • 1970-01-01
    • 2010-12-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多