保存/加载C++数据时出错

Errors with Saving/Loading C++ Data

本文关键字:出错 数据 C++ 加载 保存      更新时间:2023-10-16

所以我在这里开发这个C++资源收集小游戏。与游戏(已开发的)相关的一切都完美运行......但是,保存和加载进度对我来说似乎是一个很大的问题。

我使用了几种不同的方法,在放弃了一些自定义内容(命名您的保存文件)后,我得到了它来保存。但是现在,当加载时,它似乎使"wood"变量具有大量,并且忽略了石头,名称等内容。

法典:

  • 储蓄

    ofstream saveFile;
    saveFile.open("save.save");
    saveFile << wood << stone << iron << gold << hasHatchet << hasPickaxe << hasDrill << hasPlasma_Cutter << hatchetLevel << pickaxeLevel << drillLevel << pcutterLevel << name;
    saveFile.close();
    
  • 装载

    ifstream loadFile;
    loadFile.open("save.save");
    loadFile >> wood >> stone >> iron >> gold >> hasHatchet >> hasPickaxe >> hasDrill >> hasPlasma_Cutter >> hatchetLevel >> pickaxeLevel >> drillLevel >> pcutterLevel >> name;
    loadFile.close();
    goto mainProg;
    
  • 变量(供参考)

    int wood = 0;
    int stone = 0;
    int iron = 0;
    int gold = 0; // Resource variables
    bool hasHatchet = true;
    bool hasPickaxe = false;
    bool hasDrill = false;
    bool hasPlasma_Cutter = false; // Tool boolean values
    int hatchetLevel = 1;
    int pickaxeLevel = 0;
    int drillLevel = 0;
    int pcutterLevel = 0; // Tool level variables
    string name = "";
    

这是因为您保存的变量没有空格,所以save.sav将包含字符串000010001000。加载时,程序会将这些字符串识别为木头,这使得木变量具有那些大数

尝试在保存时用空格分隔变量

saveFile << wood << " " << stone << " " << iron << " " <<gold;