🎉 Celebrating 25 Years of GameDev.net! 🎉

Not many can claim 25 years on the Internet! Join us in celebrating this milestone. Learn more about our history, and thank you for being a part of our community!

Saving the game status

Started by
0 comments, last by wise_Guy 24 years, 3 months ago
Hey, does anyone know how to save a structure or class to a file? And how can I read it back again? I am using Windows so DOS solutions may not work? Please help me I really need to know! all the best, wiseGuy
Advertisement
Here''s how I do it. I''ll use fopen() in the example. It should be adaptable to other file i/o functions.

SaveGameClass GameToSave;

//Put all the data in the class...

FILE *SaveFile = fopen("gamesave.sav", "w+");
fwrite(&GameToSave, sizeof(SaveGameClass), 1, SaveFile);
fclose(SaveFile);

Then to read it back...

SaveGameClass GameToLoad;

FILE *LoadFile = fopen("gametoload.sav", "r+");
fread(&GameToLoad, sizeof(SaveGameClass), 1, LoadFile);
fclose(LoadFile);

That is the non OO approach, and it would only work if there was only data in the SaveGame Class. You could add the functions to the class and read in each data piece separate, or put another stucture in the class that holds all the save data and read in the stucture from within the class.

Hope I was clear enough... I need more caffeine
"The Gates of Pearl have turned to gold, it seems you've lost your way."

This topic is closed to new replies.

Advertisement