将内容从第一个文件 ("constituencies") 移动到第二个 ("temp") 并在之后重命名时,我的文件被删除

My files get deleted when moving content from the first file ("constituencies") to the second ("temp") and renaming it after

/* 我想将第一个文件("constituencies")的内容转移到第二个("temp")然后删除原来的("constituencies")文件然后重命名( ) ("temp") 到 ("constituencies") 所以它可以用一个特定的 entry/line 来刷新 从文件中删除首先找到它然后不在新生成的文件中添加它 ("temp") 然后删除旧的 ("constituencies") 文件并将 ("temp") 重命名为新的 ("constituencies") 但是我的两个文件都在这个过程中被删除了,请帮助*/

void updateOrDelete()
{
        fstream constituencies("constituencies.txt", ios::out | ios::in);
        fstream temp("temp.txt", ios::out | ios::in);
        string deleteline;
        string line;


        cout << "Which constituency do you want to remove? \n";
        cin >> deleteline;

        while (getline(constituencies, line))
        {
                if (line != deleteline)
                {
                        temp << line << endl;
                }
        }

    constituencies.close();
    temp.close();

        remove("constituencies.txt");
        rename("temp.txt", "constituencies.txt");
}

这一行fails如果temp.txt不存在:

fstream temp("temp.txt", ios::out | ios::in);

但你没有注意到,因为你没有检查它是否成功。

补救措施:

ifstream constituencies("constituencies.txt");
if(constituencies) {
    // constituencies.txt successfully opened for reading
    ofstream temp("temp.txt");
    if(temp) {
        // temp.txt successfully opened for writing
    } else {
        // fail
    }
} else {
    // fail
}

这是该函数的完整版本,其中添加了一些错误检查功能,并且移出了用户交互,因此您必须使用要从文件中删除的 deleteline 来调用它。

// returns true if successful and false otherwise
bool updateOrDelete(const std::string& deleteline) {
    static const char* orig = "constituencies.txt";
    static const char* temp1 = "temp1.txt";
    static const char* temp2 = "temp2.txt";

    // make sure you are the only one processing the file
    if(std::rename(orig, temp1) == 0) {
        // open for reading and check if it succeeded
        if(std::ifstream in(temp1); in) {
            // open for writing and check if it succeeded
            if(std::ofstream out(temp2); out) {
                std::string line;
                // do your processing
                while(std::getline(in, line)) {
                    if(line != deleteline) {
                        out << line << '\n';
                    }
                }
                // check if the new file is still in a good state before closing it
                bool success = static_cast<bool>(out);
                out.close();
                in.close();
                if(success) {
                    // all's well - move the processed file into place
                    // and remove the old (renamed) original
                    return std::rename(temp2, orig) == 0 && std::remove(temp1) == 0;
                }
                // processing failed
                // remove the corrupt/truncated file
                std::remove(temp2);
            }
        }
        // try to restore the original file
        std::rename(temp1, orig);
    }
    // if we get here, something failed
    return false;
}