(C++) Overwriting file after taking input from it?

Status
Not open for further replies.

Toshiro

In Runtime
Messages
115
I'm making a super simple text-based adventure game to test out new things I learn in my introductory C++ class.

I'm trying to implement a simple save feature where the user hits 'R' (for Record) to save to a file called "savefile.txt". I got this working OK so far. It opens (or creates if not existing) the savefile.txt file, then outputs certain variable values to the file in a specific order. It then closes savefile.txt and continues the gameplay part.

When the program starts, the user can hit 'C' (for Continue) to read values from savefile.txt. It reads them into the same variables that were outputted in the save process, and in the same exact order as they were outputted to the file so that everything matches up. The gameplay part continues from here. If savefile.txt does not exist yet or doesn't have acceptable values in it, it will give an error message and terminate.

My problem is... If I choose "continue" and get input values from savefile.txt, I can't get it to overwrite savefile.txt when I save later. It just leaves savefile.txt blank and acts like everything worked fine (I have a "failed to open" error message in place that work fine when something else is wrong).

Also, if savefile.txt already exists and has data in it (from a previous save, for example), I can start a new game and overwrite it perfectly fine, with all correct new values. It just wont let me if I've already taken input from the file during the same run.

Any ideas as to what I might be doing wrong? I can post the code for the "save" and "continue" parts, if that will help.
 
You should always post the code you are having problems with. It sounds like you may not be closing the file and trying to open it when it's already opened. However, we don't know for sure, so post your code and we'll look at it.
 
Status
Not open for further replies.
Back
Top Bottom