有没有办法使用 std::ofstream 只重写文本文件的前 30 个字符而不清除文本文件的所有其他内容?我在网上看了看,但这没有帮助。一个人提议重写整个文件,但效率很低(还有 +900*30 其他字符)。任何人?哦,这是我的代码:
void Unlock(int Level)
{
ifstream CheckIfExists("levels.txt");
if (!CheckIfExists.good())
{
SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR, "ERROR",
"Unable to open the levels file. Next level won't be unlocked!", window);
CheckIfExists.close();
return;
}
CheckIfExists.close();
if(Level >= 0 && Level <= 29)
{
ofstream New_Locks("levels.txt");
Locked[Level] = UNLOCKED;
for(int i = 0; i < 30; ++i) New_Locks << Locked[i];
New_Locks.close();
return;
}
else SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_INFORMATION, "Congratulations!",
"You won! Thank you for playing! Maybe once more?", window);
return;
}