当我写入时,为什么结构会在我的文件中消失

时间:2018-03-23 03:16:38

标签: c++ struct

每当我写一个struct到二进制文件时,任何struct在它消失之后,尽管在它之前写的任何struct仍然存在。

我在二进制输出模式下打开文件流,而我的struct只包含原始数据类型。

我还确保为每个操作创建单独的文件流。

输出:

create players
765
51

save 1
765

save 2
765
51

** struct定义**

struct player{
    int UUID;
};

保存struct s

的功能
//updates player information in the player database
bool savePlayer(player playerData){
    //count how manny playrs are in file
    // Create our objects.
    fstream countstream;
    int count = 0;
    countstream.open ("player.bin", ios::binary | ios::in);
    if(countstream.is_open()){
        countstream.seekg(0, ios::end); //set position to end
        count = countstream.tellg()/sizeof(player);
        //retuns number of players in file by getting
        //the index of the position and dividing it by the size of each player
        //no loops required :D
    }
    countstream.close();

    bool found = false;

    //if file is not empty,look through it
    if(count > 0){
        player playerTable[count];
        fstream readstream;
        readstream.open ("player.bin", ios::binary | ios::in);
        //build table
        for(int i = 0; i < count; i++){
            readstream.seekg(i, ios::beg); //set position to end
            readstream.read(reinterpret_cast <char *> (&playerTable[i]), 
                            sizeof(player));
            readstream.close();
        }

        //check table
        for(int i = 0; i < count; i++){
            if(playerTable[i].UUID == playerData.UUID){
                found = true;
                playerTable[i] = playerData;
            }
        }

        //write table back to file
        if(found){
            fstream writestream; //create writestream
            writestream.open ("player.bin", ios::binary | ios::out);
            for(int i = 0; i < count; i++){
                writestream.seekg(i, ios::beg); //set position to player
                writestream.write(reinterpret_cast <char *> (&playerTable[i]), 
                                  sizeof(player));
                if(!writestream.fail()){
                    writestream.close();
                    return true;
                }
                else{
                    writestream.close();
                    return false;
                }
                readstream.close();
            }
        }
    }

    //append if not found
    if(!found){
        fstream appendstream;
        appendstream.open ("player.bin", ios::binary | ios::out | 
                           ios::app);
        appendstream.write(reinterpret_cast <char *> (&playerData), 
                           sizeof(player));
        appendstream.close();
        if(!appendstream.fail()){
            appendstream.close();
            return true;
        }
        else{
            appendstream.close();

            return false;
        }
    }
    return false;
}

任何建议都将受到赞赏。

1 个答案:

答案 0 :(得分:-1)

你做得比实际要困难得多。您不需要将所有数据读入数组。您可以将每个项目读入变量,并检查它是否是您要替换的项目。通过在读取和写入模式下打开文件,您只需在读取文件时覆盖该文件即可。

bool savePlayer(player playerData){

    player curPlayer;
    fstream stream;
    stream.open ("player.bin", ios::binary | ios::in | ios::out);
    while(stream.read(reinterpret_cast<char*>(&curPlayer), sizeof(curPlayer))){
        if (curPlayer.UUID == playerData.UUID) {
            stream.seekg(-(sizeof curPlayer), ios::cur); // back up to location of current player
            break;
        }
    }
    stream.write(reinterpret_cast<char*>(&playerData), sizeof playerData);
    stream.close();
    return false;
}