fstream to read formatted file (gamesave)

I am creating a RPG that will take the player some time to complete. Therefor, I need to have gamesaves. Depending on if the player is in the middle of a battle, or in a menu in between battles, the structure of the gamesave file will change. The file structure would go something like this:
1
2
3
4
5
6
unit 1
name william
max-health 78
attack 45
defense 50
end-unit 1

How would I go through the file, find the names (attack,defense,name, etc) and then load the succeeding value?
Here is an idea : You can fix byte positions for each value...

Lets say bytes 1 to 4 (typically a 32 bit INT value) are reserved for the unit number (with values going from 0 to 4.294.967.295)
bytes 5 to 8 are reserved for the max-health
bytes 9 to 12 are reserved for the attack etc...

and you can reserved a defined number of bytes for strings (for example 50 bytes should be more than enough for a name...)

0|1111|222...(x50)...2222|3333|4444|5555|6666 give us a 1+4+50+4+4+4+4 = 71 bytes save file with :
- 0 the type of save format (so you can know how to parse the others bytes)
- 1 the unit number value
- 2 the name value
- 3 the max-health value
- 4 the attack value
- 5 the defense value
- 6 the end-unit value

This way you know when loading a file, that byte x to y is unit number and bytes y+1 to z is the second value etc etc etc.

Edit : if you know that the value will never go upper than 256 you can use 1 byte and not 4 etc. you need to adapt the model to fit your needs, and never change it later...
Last edited on
Use stringstream to switch from string value to int value.

just make a simple protocol, and make sure you have some sort of check to make sure the data is contiguous.

I would do something like [data : value]

edit: +1 PanGalactic.
Last edited on
This is a structured document. This seems like a perfect place to use XML:

1
2
3
4
5
6
<unit id="1">
<name>william</name>
<maxhealth>78></maxhealth>
<attack>45</attack>
<defense>50</defense>
</unit>


You just need to define an XML Schema which which can adequately represent the saved game structure, pick a suitable XML library, and then write the code to query and create XML DOM.

I would recommend that, at least to start, you pick save points in the game rather than letting them save the state at any point. This will simplify the structure of what you need to save.
Thanks! XML looks great! Are there any XML libraries you would recommend?
I've used Xerces, libxml2 and, most recently, TinyXML.

http://xerces.apache.org/xerces-c/
http://www.xmlsoft.org/
http://library.gnome.org/devel/libxml++-tutorial/stable/
http://www.grinninglizard.com/tinyxml/
http://code.google.com/p/ticpp/

I liked TinyXML++ the best, it worked for my needs but does not appear to be actively maintained.
Thanks, TinyXML looks like it would fit my needs best. It appears relatively simple, which is great for me. Thanks for your help!
Topic archived. No new replies allowed.