Advertisement

Saving things

Started by October 06, 2000 11:42 AM
3 comments, last by Ratman 24 years, 3 months ago
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
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.

Advertisement
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

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?
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.

This topic is closed to new replies.

Advertisement