如何在 C++ 中从 txt 文件中删除一行而不丢失文件内的其他数据

问题描述 投票:0回答:1

我正在尝试创建一个待办事项列表程序。

我在从txt文件中删除任务时遇到问题,我尝试从程序中读取所有数据并将某些数据替换为空白,但最终仍然留下了一些字符。

如何从文件中完全删除该特定行?

例如,我只想删除:

**1。 A,A,未完成,2023 年 8 月 20 日星期日 22:37:33 **(这个)

2. B,B,未完成,2023 年 8 月 20 日星期日 22:37:40

我已经尝试过了,但它没有按照我想要的方式工作

bool DoList::Remove_Task()
{
    std::ofstream temp_file("temp.txt", std::ios::app);
    std::ifstream file("Tasks_Data.txt");
    if (!temp_file.is_open())
    {
        std::cerr << "temp File cannot be opened to modify";
        return false;
    }
    else if (!file.is_open())
    {
        std::cerr << "File cannot be opened to read";
        return false;
    }
    else
    {
    int ID;
    int search;
    std::string TaskName;
    std::string Description;
    std::string Status;
    char str[26];
    std::string line;

    std::cout << "Enter Your Task ID to remove: ";
    std::cin >> search;

    while (getline(file, line))
    {
        while(file >> ID)

        if (ID == search)
        {

            ID = 0;
            TaskName = "\0";
            Description = "\0";
            Status = "\0";
            strcpy_s(str, "\0");
            temp_file << ID <<  ". " << std::setprecision(5) << TaskName << ", " << std::setprecision(5) << Description << ", " << std::setprecision(5) << Status << ", " << std::setprecision(5) << str << std::endl;
    
        }
        else
        {
            temp_file << line;
        }


    }
    }
    file.close();
    temp_file.close();

    remove("Tasks_Data.txt");
    rename("temp.txt", "Tasks_Data.txt");

    

    return true;
}
c++ file-handling
1个回答
1
投票

原代码:

std::ofstream temp_file("temp.txt", std::ios::app);

文件已打开以进行追加。如果先前的实例崩溃,文件将包含先前运行的数据。只需打开它即可写入。

原代码:

while (getline(file, line))
{
    while(file >> ID)

您已经读完了整行。

file >> ID
将从下一行读取ID。解析
line
以确定其 id。例如,您可以使用 std::stringstream:

int task_id;
std::stringstream(line) >> task_id;

原代码:

temp_file << ID <<  ". " << std::setprecision(5) << TaskName << ", " << std::setprecision(5) << Description << ", " << std::setprecision(5) << Status << ", " << std::setprecision(5) << str << std::endl;

对于不删除的行,您不需要解析它们并生成新的行。只需写下整行:

temp_file << line << std::endl;

原代码:

        return false;
    }
    else if (

如果前一个块从函数返回,则不需要

else
部分。

原代码:

    remove("Tasks_Data.txt");
    rename("temp.txt", "Tasks_Data.txt");

您不需要删除旧文件。

rename()
将原子地用新的替换它。

整个函数可能是这样的(

id
删除作为参数传递的参数,不检查格式错误):

bool Remove_Task(int id)
{
    std::ofstream temp_file("temp.txt");
    if (!temp_file.is_open())  {
        std::cerr << "temp File cannot be opened to modify";
        return false;
    }

    std::ifstream file("Tasks_Data.txt");
    if (!file.is_open()) {
        std::cerr << "File cannot be opened to read";
        return false;
    }

    std::string line;

    while (getline(file, line))
    {
        int task_id;
        std::stringstream(line) >> task_id;

        if (task_id != id) {
            temp_file << line << std::endl;
        }
    }

    file.close();
    temp_file.close();

    rename("temp.txt", "Tasks_Data.txt");

    return true;
}
© www.soinside.com 2019 - 2024. All rights reserved.