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

C++中使用fstream删除存储在文件中的vector元素的实现方法

问题核心原因

你的代码无法实现文件中元素删除,是以下几个逻辑缺陷导致的:

  • 程序启动时没有从practiceFile.txt读取已存储的学生名单到Names向量中,执行删除操作时Names是空数组,自然找不到要删除的元素
  • 文本文件不支持原地删除某一行内容的操作,你当前仅用追加模式打开文件,修改完向量后没有把更新后的全量数据重新写入文件,文件内容不会有任何变化
  • 新增学生的逻辑存在重复写入问题:每次新增单个学生就把整个Names向量全部追加到文件末尾,会导致文件中存在大量重复的姓名数据

实现方案

要通过fstream实现文件存储的vector元素删除,按以下流程执行即可:

  1. 程序启动时先读取文件中所有已存的姓名,加载到Names向量中
  2. 执行增/删操作修改Names向量
  3. 操作完成后清空原文件内容,把更新后的Names向量全量写入文件
  4. 查看全量学生时直接读取更新后的文件内容即可

修正后可运行代码

#include <algorithm>
#include <iostream>
#include <string>
#include <vector>
#include <fstream>
using namespace std;

void Menu()
{
    cout << "******************" << endl;
    cout << "1 - Add student" << endl;
    cout << "2 - Delete student" << endl;
    cout << "3 - All students" << endl;
    cout << "******************" << endl;
}

struct School
{
    string Name;
    string remove;
    int Ages;
    int Option;
};

// 加载文件中的姓名到vector
void loadNamesFromFile(vector<string>& names, const string& filename) {
    ifstream inFile(filename);
    string line;
    if (inFile.is_open()) {
        while (getline(inFile, line)) {
            if (!line.empty()) {
                names.push_back(line);
            }
        }
        inFile.close();
    }
}

// 把vector中的姓名全量写入文件(覆盖原有内容)
void writeNamesToFile(const vector<string>& names, const string& filename) {
    ofstream outFile(filename, ios::trunc); // trunc模式清空原有内容
    if (outFile.is_open()) {
        for (const auto& name : names) {
            outFile << name << endl;
        }
        outFile.close();
    }
}

int main()
{
    const string filename = "practiceFile.txt";
    School Students;
    vector <string> Names;
    // 先加载已有数据
    loadNamesFromFile(Names, filename);

    Menu();
    cout << endl;
    cout << "Option: ";
    cin >> Students.Option;
    cin.ignore();

    if(Students.Option == 1)
    {
        cout << "Enter name: ";
        getline(cin, Students.Name);
        Names.push_back(Students.Name);
        // 写入更新后的全量数据
        writeNamesToFile(Names, filename);
    }
    else if(Students.Option == 2)
    {
        cout << "Enter name: ";
        getline(cin, Students.remove);
        // 查找并删除元素
        auto itr = find(Names.begin(), Names.end(), Students.remove);
        if(itr != Names.end())
        {
            Names.erase(itr);
            // 写入更新后的全量数据
            writeNamesToFile(Names, filename);
            cout << "Delete success" << endl;
        } else {
            cout << "Name not found" << endl;
        }
    }
    else if(Students.Option == 3)
    {
        string line;
        ifstream NewFile(filename, ios::in);
        while(getline(NewFile, line))
        {
            cout << line << endl;
        }
        NewFile.close();
    }

    return 0;
}

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.30 15:36:02