保存/加载C++数据时出错
Errors with Saving/Loading C++ Data
所以我在这里开发这个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;
相关文章:
- 将数据从 std::Vector 存储到 Eigen::Vector 时出错
- 为什么双精度数据类型在C++中出错?
- 将私有数据成员作为默认参数传递给该类的公共方法时出错
- 将运算符<<与隐式转换的非基本数据类型一起使用时出错
- 使用来自串行端口的字符串数据来操作振镜扫描仪在更高的速度下会出错
- 如何对 int 变量应用验证,使其仅接受整数数据,并且在任何其他数据的情况下不会出错?
- 为什么当我们把变量"u"的数据类型从长整型更改为整型时,模幂会出错?
- 尝试将数据添加到结构内部的矢量时出错
- 使用提升库在队列和堆栈数据结构上保存和加载数据时出错
- 尝试从 csv 文件输入大量数据时出错
- 为什么我在尝试从istringstream读取数据时出错
- 保存/加载C++数据时出错
- 使用矢量<数据类型>进行 arm 编译时出错
- C++中矢量中的数据存储出错
- C++中的模板仅在长数据类型上出错
- 为什么我在尝试将opencv图像数据指针转换为char*时出错
- C++executeQuery()在显示表中的MySQL数据时出错
- 使用 Boost::asio::async_send_to 发送加密数据时出错
- C++在尝试编写对象读取器时,在尝试从一个向量获取数据时出错
- 创建缓冲区以加载数据时出错