You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何实现文件的按需追加写入与覆盖写入?

解决C++文件追加与覆盖写入的切换问题

核心问题分析

你的代码中,用于写入名称列表的Display_File一开始就以ios_base::app(追加)模式打开,无论后续是添加新条目还是删除条目,都复用这个流。删除操作时,处理后的vector内容会被追加到原文件末尾,而非覆盖原内容,最终导致文件内容重复。

解决方案

将添加和删除操作的文件打开逻辑分离,根据操作类型选择对应的打开模式:

  • 添加新条目:使用ios_base::app模式打开文件,追加写入新名称。
  • 删除条目:读取文件内容到vector并完成删除后,关闭原有流,再以覆盖模式(默认ofstream打开即截断原文件)重新打开文件,写入处理后的vector内容。

修改后的代码片段

void create_password()
{
    string holder;
    string keeper;
    vector<string> delete_entry;
    vector<string>::iterator new_end;
    // 提前拼接目标删除字符串,避免重复计算
    string target_entry = "-- " + new_password_name + " --";

    ofstream New_PasswordFile(new_password_name + ".txt"); // 创建对应密码文件

    // 单独处理添加新条目逻辑:仅在需要追加时打开流
    if (is_same_entry != true) {
        ofstream Display_File(stored_username + "names.txt", ios_base::app);
        if (Display_File.is_open()) {
            Display_File << "\n-- " << new_password_name << " --\n";
            Display_File.close(); // 写完立即关闭流
        }
    }

    cout << "WHAT IS THE PASSWORD?";
    cin >> new_password;

    if (new_password == "DELETE") {
        new_password = "";

        // 读取文件内容到vector
        ifstream Display_File_Checker(stored_username + "names.txt");
        if (Display_File_Checker.is_open()) {
            while (getline(Display_File_Checker, holder)) {
                delete_entry.push_back(holder);
            }
            Display_File_Checker.close();
        }

        // 删除目标条目:std::remove一次即可处理所有匹配项
        new_end = remove(delete_entry.begin(), delete_entry.end(), target_entry);
        delete_entry.erase(new_end, delete_entry.end());

        // 以覆盖模式打开文件(默认ofstream打开会截断原文件)
        ofstream Display_File(stored_username + "names.txt");
        if (Display_File.is_open()) {
            // 用范围for循环遍历,代码更简洁
            for (const string& line : delete_entry) {
                cout << "keeper :" << line << endl;
                Display_File << line << endl;
            }
            Display_File.close();
        }
    }
}

关键修改说明

  1. 分离操作的流打开逻辑:添加操作仅在需要时打开追加模式流,写完立即关闭,避免后续删除操作复用该流。
  2. 删除时覆盖写入:读取完成后关闭输入流,重新以默认覆盖模式打开输出流,直接写入处理后的vector,彻底替换原文件内容。
  3. 优化删除逻辑:std::remove会将所有匹配元素移到容器末尾,无需循环判断,一次调用即可完成批量删除。
  4. 提前拼接目标字符串:避免重复计算,同时确保字符串拼接的正确性(string类型与const char*的拼接)。

内容的提问来源于stack exchange,提问作者That_Coder

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.30 06:12:32