Jump to content

  • Log In with Google      Sign In   
  • Create Account


save and read data from file


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
10 replies to this topic

#1 hit   Members   -  Reputation: 113

Like
0Likes
Like

Posted 30 October 2012 - 09:20 PM

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

Edited by hit, 30 October 2012 - 10:44 PM.


Sponsor:

#2 rnlf   Members   -  Reputation: 1057

Like
0Likes
Like

Posted 31 October 2012 - 12:17 AM

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.

my blog (German)


#3 lorangrim   Members   -  Reputation: 103

Like
0Likes
Like

Posted 31 October 2012 - 02:37 AM

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).
#*# Find my c++ sockets library and other stuff @ http://www.alhem.net/ *#*

#4 rnlf   Members   -  Reputation: 1057

Like
1Likes
Like

Posted 31 October 2012 - 03:59 AM

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


Sadist. Don't do this to people Posted Image

my blog (German)


#5 BeerNutts   Crossbones+   -  Reputation: 2522

Like
1Likes
Like

Posted 31 October 2012 - 12:48 PM

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)

#6 Ectara   Crossbones+   -  Reputation: 2745

Like
0Likes
Like

Posted 31 October 2012 - 02:22 PM

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.

#7 Lauris Kaplinski   Members   -  Reputation: 841

Like
0Likes
Like

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.


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/

#8 ChaosEngine   Crossbones+   -  Reputation: 2116

Like
0Likes
Like

Posted 31 October 2012 - 04:21 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.
if you think programming is like sex, you probably haven't done much of either.-------------- - capn_midnight

#9 BeerNutts   Crossbones+   -  Reputation: 2522

Like
0Likes
Like

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.


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)

#10 Lauris Kaplinski   Members   -  Reputation: 841

Like
0Likes
Like

Posted 01 November 2012 - 02:43 AM

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/

#11 ChaosEngine   Crossbones+   -  Reputation: 2116

Like
0Likes
Like

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.


My bad. When I read the OP I somehow assumed that he was just talking about a config file (he did mention that, but also save games).

Agree that for save games, large state, etc. human readable is probably not what you want.
if you think programming is like sex, you probably haven't done much of either.-------------- - capn_midnight




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS