Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

Ratman

Saving things

This topic is 6496 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I learned C++ pretty much on my own, and I never really got much into file manipulation. I mean, I can do simple reading/writing to a text file. But thats pretty much it. The few games Ive written before, if I want to save off some information to a file (a map, some character properties, etc) I always end up putting it in a text file. Like, I might on the first line write the characters name, then the next line his hit points, next one save his strength or whatever. Then I have my game read each line one at a time to get these stats back in. But now Im thinking.. theres gotta be an easier way. Or at least a cleaner way. Or I''d really like to get away from a plain text file so they arent so easily modified. I dont know, can I just write an instance of a class to a (non-text_ file, then load it back up? Writing the stuff like I am now is killing me. Thanks ratman --------------- http://ratfest.org

Share this post


Link to post
Share on other sites
Advertisement
Guest Anonymous Poster
Yes, you can write class instances to a file. Yes, you can read them back in and maintain references and such. There''s not enough room here to explain how to do it. Search for "persistant objects" on AltaVista. Or maybe, "perserving persistant objects". There''s also a book called _Code Secrets_ by Tom Swan that explains how to do this. Probably in other books too.

Share this post


Link to post
Share on other sites
You could use binary file access, this will allow you to write out and read in data members without converting them to ASCII first. Binary files also enable you to use random file access and other useful stuff, they also take up less space in general than text ones.

------------------------------
#pragma twice


sharewaregames.20m.com

Share this post


Link to post
Share on other sites
Crikey! That''s exactly what I *want*! At the moment my files get saved as gibberish (all squiggles and spaces) and because I haven''t written a level editor yet I can''t edit them directly, so I have to use code. How are you saving your stats so they are easily changed? Do you use sprintf?

Share this post


Link to post
Share on other sites
Here''s how I do it:

    
struct actor
{
int x;
int y;
};

void main()
{
FILE* fp=NULL;

// to save a file use "wb"

// to read a file use "rb"

if((fp=fopen("actor.sav", "wb"))==NULL)
{
return 0;
}

actor Actor;
Actor.X=10;
Actor.Y=20;

// there''s two ways to do this

// first you can do this

// to save a file use fwrite

// to read a file use fread

fwrite(&Actor, sizeof(actor), 1, fp);

// or you can do this

fwrite(&Actor.X, sizeof(int), 1, fp);
fwrite(&Actor.Y, sizeof(int), 1, fp);

// make sure you close the file

fclose(fp);
}


I don''t like saving with the first method any more. Some structs have byte padding for who knows why. So if you try to read in a save file that someone else made using their exect same structure, it may not read in correctly. So I decided to use the latter of the two. The latter uses more lines, but you''re guranteed to retrieve the data the same as it was written.

Good luck with your games.

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!