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
Edited by hit, 30 October 2012 - 10:44 PM.
Posted 30 October 2012 - 09:20 PM
Edited by hit, 30 October 2012 - 10:44 PM.
Posted 31 October 2012 - 12:17 AM
my blog (German)
Posted 31 October 2012 - 02:37 AM
Posted 31 October 2012 - 12:48 PM
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");
}
}
Posted 31 October 2012 - 02:22 PM
should be fread().fwrite(pPlayerState, sizeof(TPlayerState), 1, fLoadFile);
Posted 31 October 2012 - 03:37 PM
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.
Posted 31 October 2012 - 04:21 PM
Posted 31 October 2012 - 09:23 PM
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.
Posted 01 November 2012 - 02:43 AM
libxml2 has clean C interface so no need for C++ here.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.
Posted 01 November 2012 - 02:30 PM
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.