Saving the game status
Okay fellas, the $64,000,000 question(s):
a) How do I write an entire structure to a file?
b) How do I read that data back into my structure?
This is for stuff like highscores.dat, etc.
BTW I am using Windows 95/98 and DirectX so I don''t think DOS solutions will work...
Thanks in advance,
wiseGuy;
To write a struct to a file is easy, if you dont want it algorithmed that is...
C follows
---------------------------------------
FILE *fp //File pointer
HIGHSCORE score;
fp=fopen("test.dat", "bw") //Open file in binary
//mode, write only
fwrite(&score,sizeof(HIGHSCORE),1,fp);
fclose(fp);
---------------------------------
C ends
Hope that helped!
and when u read data, just fopen with "br" instead of "bw"
and replace fwrite with fread...
========================
Game projects:
www.fiend.cjb.net
so wheres my $64,000,000?
Edited by - JonatanHedborg on 3/24/00 4:16:21 AM
C follows
---------------------------------------
FILE *fp //File pointer
HIGHSCORE score;
fp=fopen("test.dat", "bw") //Open file in binary
//mode, write only
fwrite(&score,sizeof(HIGHSCORE),1,fp);
fclose(fp);
---------------------------------
C ends
Hope that helped!
and when u read data, just fopen with "br" instead of "bw"
and replace fwrite with fread...
========================
Game projects:
www.fiend.cjb.net
so wheres my $64,000,000?

Edited by - JonatanHedborg on 3/24/00 4:16:21 AM
=======================Game project(s):www.fiend.cjb.net
If you do care about portable data (i.e. saving game under Windows, and loading it under unix, etc...), you should be carefull with structs, because C/C++ compilers do some alignment within struct (-zp# option usually), and if you
have two binaries with different aligment, than loaded data will not fit in the structure as suposed...
To avoid that either use same struct aligment, or save each
variable from struct, or do some auto-struct calibration before load in code...
(very likely you don''t care about it, but hey, does it hurt to know MORE ?
---------------------------------------------------
Ped - Peter Helcmanovsky - 7 Gods demo group
http://7gods.rulez.sk
First Sight Entertainment - http://members.xoom.com/fseteam
---------------------------------------------------
have two binaries with different aligment, than loaded data will not fit in the structure as suposed...
To avoid that either use same struct aligment, or save each
variable from struct, or do some auto-struct calibration before load in code...
(very likely you don''t care about it, but hey, does it hurt to know MORE ?

---------------------------------------------------
Ped - Peter Helcmanovsky - 7 Gods demo group
http://7gods.rulez.sk
First Sight Entertainment - http://members.xoom.com/fseteam
---------------------------------------------------
---------------------------------------------------Ped - Peter Helcmanovsky - 7 Gods demo grouphttp://7gods.rulez.skFirst Sight Entertainment - http://members.xoom.com/fseteam---------------------------------------------------
Just thought I would mention...when you just copy a structure to a file watch out for pointers, etc.. when you save the structure it will save the pointer value but not what its pointing to...also if you load it up later, the pointer value will probably not be valid - who knows what it may be pointing to..so keep an eye out for that...
Okay, now I have done the method suggested above, and it Appears to work, however:
When I run the game from the compiler (Compile + Run), the file functions work (read and write).
BUT when I run the game from windows (without the compiler), the save files are not imported.
I got around this by excluding the Binary mode "b" operator when doing fopen, but will this have any adverse effects in the long term?
btw Ped - at present I don''t *really* care its true, but It''s good to have such knowledge for the future. But i thought you needed different versions of a program for windows/unix?
When I run the game from the compiler (Compile + Run), the file functions work (read and write).
BUT when I run the game from windows (without the compiler), the save files are not imported.
I got around this by excluding the Binary mode "b" operator when doing fopen, but will this have any adverse effects in the long term?
btw Ped - at present I don''t *really* care its true, but It''s good to have such knowledge for the future. But i thought you needed different versions of a program for windows/unix?
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement
Recommended Tutorials
Advertisement