从二进制文件C ++中删除对象

有类似的问题,但c ++中没有。 我有class player,它的数据写在二进制文件中,我想按其名称删除player(obj)。

void Developer:: delete_player(string ingame_name)
{   
    fstream file; Player plyr; fstream file2;
    try 
    {
        file.open("player_database.dat", ios::binary | ios::in); 
        if (!file.is_open())
            throw exception("could not open file");
        try
        {
            file2.open("temp.dat", ios::out | ios::binary); 
            if (!file.is_open())
                throw exception("could not open file");
            while (!file.eof()) 
            {
                file.read((char*)&plyr, sizeof(Player)); // read each object
                if (plyr.ingame_name != ingame_name)
                    file2.write((char*)&plyr, sizeof(Player)); 
            }
            file.close();
            file2.close();
            remove("player_database.dat");
            rename("temp.dat", "player_database.dat");
        }
        catch (std::exception& e)
        {
            cout << "error while opening the file";
        }
    }
    catch (std::exception&e)
    {
        cout << "error while opening the file";
    }
}

除调试外,没有构建错误。 它将打开一个新的头文件xmemory,并引发异常:读取访问冲突。 _Pnext为0x13。发生了

评论