Something Is Wrong With My File Saving, But It Looks Fine!

So ive been making a text adventure game on c++, and tbe saving mechanics work perfectly, but the text file doesnt show the equipment and other stats categories, where does my code get the stats from if the file is empty there? What caused this? Ive searched around my code but nothing seems wrong. Is there a way to fix this? I will send my repl.
Repl link:
image of problem

When saving files you must take note of two things:

  1. The write function is responsible for loading data from your save file.
  2. The modify function is responsible for saving that data.

So, when I look at your write function, you don’t have any code to load offhand , armor , head , or xp[0] from the file.
The same thing happens with your modify function. There’s no code to save offhand , armor and head .

Fix that up and try again

1 Like

Thats because i havent inppemented them yet. Theyre just there as placeholders for now.

At the end of your .txt file I saw the “Equipments” session and “Other Stats” session too.

That’s why I tought you were meaning these (which you did not implement yet)…

Right here:


Well, yes. But only the stick is meant to show because i havent implemented offhand and armor stuff. The problem is that after the inventory, nothing else is saved. I also found out that the bug happens when in combat.

I get it now…

Well in that case it’s a bit more complex.

Try to check how the file stream is before you write in your modify function. You can do this by using savefile.good(). If this check fails, you should reset the file stream using savefile.clear()

For example:

void modify(string filename, bool newsave){, ios::out);
        savefile.clear(); // Clears any error states
        // And you can handle the error too, like, you could log an error message or attempt to reopen the file


Another thing is, try to add print statements before and after combat to display the values that are important for saving, this way you can confirm whether the values are being correctly updated during combat and if they’re ready to be saved

Like before combat:

cout << "Before combat - Health: " << health[0] << ", XP: " << xp[0] << endl;

And after combat:

cout << "After combat - Health: " << health[0] << ", XP: " << xp[0] << endl;

Thanks, but i just fund out i can use json files instead of text files to make things simpler to edit. Thanks for the scripts tho, im making my project again from scratch.


This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.