save and read data from file

Started by
9 comments, last by ChaosEngine 11 years, 5 months ago
how do I store variables/data in a file, that
can be read later, for example a save file for a game


I can create, write, read from files using c,c++ & win32 but cant find how I would
simulate a config/save file
Advertisement
What you are looking for is some kind of serialization method. There are plenty of possibilities out there. Some popular ones are Protocol Buffers and boost::serialization. You may do it yourself, but it involves a bit of trickery like the factory pattern or similar which may be a bit much for beginners. I personally would recommend Protocol Buffers, they are easy to use but require an additional compile step (running the protoc compiler and creating data classes with it). Boost::serialization has it's own difficulties but using plain C++ without any additional tools.

If you just want to read some configuration, you might also take a look at YAML (using yaml-cpp) which allows you to read YAML files in just a few lines of C++ code.
You should also consider if you want the file to be human readable, as is most certainly the case with a configuration file.

Then you should avoid a binary format and instead use something like .yml or even .xml (which I sort of prefer).
[size=2]#*# Find my c++ sockets library and other stuff @ http://www.alhem.net/ *#*

...to be human readable...
... or even .xml (which I sort of prefer).


Sadist. Don't do this to people unsure.png
If you want to do it in C (as the title states), then boost is out of the question.

The easiest way is to just writes your structures to file and read them back. Something like this:

typedef struct {
int PlayerHealth;
int PlayerX, PlayerY;
int Strength;
int Health;
} TPlayerState;

// Save a players state to file
void SaveState(TPlayerState PlayerState)
{
// Open the file for writing binary
FILE *fSaveFile = fopen("SaveFile.bin", "wb");

if (fSaveFile) {
// Write the structure to the file
fwrite(&PlayerState, sizeof(TPlayerState), 1, fSaveFile);
fclose(fSaveFile);
}
else {
printf("Error opening savefile!\n");
}
}

// Loads a players state. You'd call this with a pointer to the Player's State, like this:
// RestoreState(&PlayerState);
void RestoreState(TPlayerState *pPlayerState)
{
// Open the file for reading binary
FILE *fLoadFile = fopen("SaveFile.bin", "rb");

if (fLoadFile) {
// read the structure from the file
fwrite(pPlayerState, sizeof(TPlayerState), 1, fLoadFile);
fclose(fLoadFile);
}
else {
printf("Error opening savefile!\n");
}
}


Good luck and have fun!

My Gamedev Journal: 2D Game Making, the Easy Way

---(Old Blog, still has good info): 2dGameMaking
-----
"No one ever posts on that message board; it's too crowded." - Yoga Berra (sorta)

Aside from the obvious endian-ness and padding issues, which I'm sure are beyond the scope of this thread,


fwrite(pPlayerState, sizeof(TPlayerState), 1, fLoadFile);

should be fread().

I just want to make sure he knows that when he wants to start distributing this game to others or using it on a different computer, he'll have to pick a different method.

Aside from the obvious endian-ness and padding issues, which I'm sure are beyond the scope of this thread,

I just want to make sure he knows that when he wants to start distributing this game to others or using it on a different computer, he'll have to pick a different method.


The layout of the file will be dependent on build system (compiler/linker configuration, including machine architecture) only. For all binaries built with the same system the file is portable. With little care (ensure identical padding) the file can be made to be portable for between Win/Mac builds. C binary ABI is pretty well standardized, unlike C++.

The only remaining (realistic) incompatibilites will be between 32/64 bit systems and between x86/ARM endianness.
Lauris Kaplinski

First technology demo of my game Shinya is out: http://lauris.kaplinski.com/shinya
Khayyam 3D - a freeware poser and scene builder application: http://khayyam.kaplinski.com/
What platforms do you want to target? Any reason you really have to use C?

Not that it's not possible to do it in C, but it's a lot easier in C++. Even if you want to write the rest of your app in C, you could just wrap a C++ lib (like tinyxml) and define a C interface.

I wouldn't recommend a binary format. Aside from potential portability issues, you're just creating pain for yourself. Make your save format human readable, and more importantly human editable. This will allow you to change config values as you go.

XML is probably the most popular format. It's not necessarily the best, but it has the largest support in terms of tooling, knowledge base, etc.
if you think programming is like sex, you probably haven't done much of either.-------------- - capn_midnight

What platforms do you want to target? Any reason you really have to use C?

Not that it's not possible to do it in C, but it's a lot easier in C++. Even if you want to write the rest of your app in C, you could just wrap a C++ lib (like tinyxml) and define a C interface.

I wouldn't recommend a binary format. Aside from potential portability issues, you're just creating pain for yourself. Make your save format human readable, and more importantly human editable. This will allow you to change config values as you go.

XML is probably the most popular format. It's not necessarily the best, but it has the largest support in terms of tooling, knowledge base, etc.


I'm just going to disagree with this. If you're saving a large state, having it human readable truly gains you nothing except massive bloat. Writing as binary should really be the way to go for a game. If you're making an engine, or a level editor, sure, use a standardized method to save the files, but not for internal game loading/saving. It's going to be much easier reading and writing binary data.

Personally, I would use xml for setting up characters, weapons, controller configurations, but NOT for internal states.

My Gamedev Journal: 2D Game Making, the Easy Way

---(Old Blog, still has good info): 2dGameMaking
-----
"No one ever posts on that message board; it's too crowded." - Yoga Berra (sorta)


What platforms do you want to target? Any reason you really have to use C?

Not that it's not possible to do it in C, but it's a lot easier in C++. Even if you want to write the rest of your app in C, you could just wrap a C++ lib (like tinyxml) and define a C interface.

libxml2 has clean C interface so no need for C++ here.

But I agree with BeerNutts that XML is not proper format for in-engine state saving. XML is meant for data interchange, not storage.
Lauris Kaplinski

First technology demo of my game Shinya is out: http://lauris.kaplinski.com/shinya
Khayyam 3D - a freeware poser and scene builder application: http://khayyam.kaplinski.com/

This topic is closed to new replies.

Advertisement