How to create a "save file" (preferably in .dat or .sav format)

Mar 1, 2009 at 6:36pm
Hello, I've been making a Zork inspired text/RPG game in C++ and right now it's at about 1100 lines of code, and therefore getting to be about 20-25 minutes long. I want to know how I can make the program recognize a format so it can save and load game files so the player can continue his/her game. I'm using just the basic headers (it's pretty much a DOS game with some embellishments), plus an Allegro library as there are pop-up pictures of the monsters you encounter before the fight, and they bounce when you hit them, ect. Anyway, I just want to know if there's a way. There are only about 10 character/storyline specific variables, so if I could get the program to save that data into a file and then load it back next time it starts up, that would be WONDERFUL.

Thanks in advance.
Mar 1, 2009 at 7:02pm
You can use the normal fstreams, and give your file any extention you want. The program will read it as a text file. Well, I shouldn't say that, because I'm not completly sure. I recommend you to use .dat (isn't edited by normal users, but you can do whatever you want with it by opening it in notepad).
See for fstream: http://www.cplusplus.com/doc/tutorial/files.html

I would use one file, for example "oblivia.dat" (name the same as your game).

You will have to write two functions; one to read from and one to write to the file. How this is done, is decided by you. As long as the write-function writes the same output as the read-function expects as input.
Mar 1, 2009 at 7:03pm
Save all the variable values to the file in a format you know so it would be easier to parse when loading it

eg:
1
2
3
4
5
6
7
8
9
//Variables you need to save:
int a_variable;
string another_variable;

void save()
{
    ofstream file("save.sav");
    file >> a_variable >> '\n' >> another_variable;//Save them in different lines
}
Topic archived. No new replies allowed.