gpt4 book ai didi

c++ - 从文件中删除特定记录(或数据)

转载 作者:行者123 更新时间:2023-12-01 14:52:58 25 4
gpt4 key购买 nike

我想从文件中删除用户输入的记录(如果存在)。
我编写了以下代码以将数据添加到文件中:-
谁能告诉我应采用哪种方法从文件中删除特定记录(或数据)?谢谢!

void addData(char* name, char* data){

ifstream input_file(name);
ofstream output_file(name, ofstream::app);

if (!input_file) {
output_file << data << endl;
}
else {
output_file << data << endl;
cout << "This is the first data of this person" << endl;
}

input_file.close();
output_file.close();

}

最佳答案

基本上有两种方法。

  • 打开源文件->将所有数据读入内存->关闭源文件->在内存中进行修改->以覆盖模式打开源文件->写入新数据。
  • 使用一个临时文件。 ->打开用于输入的源文件->打开用于输出的临时文件->逐行读取源文件->立即对每个读取的行进行修改->将结果写入临时文件->在读取所有源文件数据之后,关闭bot文件->删除原始源文件。将临时文件重命名为旧的源文件名

  • 还有更多解决方案,但是经常使用这两种方法。

    请查看一些示例代码。只是简单的例子。没有生产代码。没有错误检查。只是给您一个想法,它如何工作。

    方法1
    #include <iostream>
    #include <fstream>
    #include <string>
    #include <vector>

    std::vector<std::string> readFile(const std::string& filename) {

    // Here we will store all the data from the file
    std::vector<std::string> fileData;

    // Open the source file
    std::ifstream fileStream(filename);

    // Read line by line and add it to our fileData
    std::string line;
    while (std::getline(fileStream, line)) {

    fileData.push_back(line);
    }
    return fileData;
    }

    void writeFile(std::vector<std::string>& fileData, const std::string& filename) {

    // Open file for output
    std::ofstream fileStream(filename);

    // Write all data to file
    for (const std::string& line : fileData)
    fileStream << line << '\n';
    }

    int main() {

    // Aproach with read complete file to local variable, modify and the store again
    const std::string dataFileName("r:\\test.txt");

    // Get file content
    std::vector<std::string> data = readFile(dataFileName);


    // Now go through all records and do something
    for (std::string& line : data) {

    // If some condition is met then do something, for example modify
    if (line == "Line1") line += " modified";
    }


    // And then write the new data to the file
    writeFile(data, dataFileName);

    return 0;
    }

    方法2:
    #include <iostream>
    #include <fstream>
    #include <string>
    #include <cstdio>

    int main() {
    // Aproach with temp file, remove and rename, and on the fly change

    const std::string dataFileName("r:\\test.txt");
    const std::string tempFileName("r:\\temp.txt");

    {
    // Open the source file with data
    std::ifstream dataFileStream(dataFileName);

    // Open the temporary file for output
    std::ofstream tempFileStream(tempFileName);

    // Now read the source file line by line with a simple for loop
    std::string line;
    while (std::getline(dataFileStream, line)) {

    // Identify the line that should be deleted and do NOT write it to the temp file
    if (line != "SearchString") { // Or any other condition

    // Write only, if the condition is not met
    tempFileStream << line << '\n';
    }
    }
    } // The end of the scope for the streams, will call their destructor and close the files

    // Now, remove and rename
    std::remove(dataFileName.c_str());
    std::rename(tempFileName.c_str(), dataFileName.c_str());

    return 0;
    }

    关于c++ - 从文件中删除特定记录(或数据),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61193965/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com