【发布时间】:2021-01-18 13:03:53
【问题描述】:
所以我正在制作一个非常简单的猜谜游戏机,我想将数据永久存储在一个文件中(高分)。但是每次我编译我正在使用的文件时都会清空自己。有没有办法阻止它? 我尝试了很多没有用的东西,老实说,我不知道问题出在哪里。我猜这与鳍和鳍有关,但对其他人来说似乎有效
#include <iostream>
#include <fstream>
#include <time.h>
#include <conio.h>
int hs;
//this would be the play_game() function, unrelated to the subject
int main()
{
std::ofstream fout;
fout.open("HS.txt");
std::ifstream fin;
fin.open("HS.txt");
srand(time(NULL));
//menu with 4 options, play, quit, help and highscore (which i'm working on)
fin.close();
fout.close();
}
【问题讨论】:
-
检查
std::fstream::open(),mode参数和std::ios::app具体。 -
@πάνταῥεῖ:这个问题不是重复的,因为 OP 不只是想要追加,他们想要读取和写入。
-
如 panta rei 所说,将您的
fout.open命令修改为fout.open ("HS.txt", std::ofstream::out | std::ofstream::app); -
@Menelaus 要处理同一个文件资源的读取和写入,只需使用一个
std::fstream,否则您需要同步您的fout和fin通过使用相同的rdbuf()指针。