Figuring out how to do LOAD/SAVE game...

Started by
4 comments, last by Vanz 22 years, 1 month ago
Hi, I am trying to set up the LOAD and SAVE game portion of my game. I have several structures set up similar to: typedef struct _KeepStruct { int x,y, MonsterType[3], MonsterQuantity[3], KeepPicture; BOOL IsKeep, Explored; int Gold, Treasure, TurnsTillRestock; } _KeepStruct ; _KeepStruct Keep[30]; I save all my structures like this... stream = fopen( "save.txt", "w+" ); fwrite(Country,sizeof(struct _KeepStruct),66,stream); fwrite(Knight,sizeof(struct _KnightStruct),54,stream); fwrite(Castle,sizeof(struct _CastleStruct),8,stream); fclose(stream); Then try to load them back like this.... stream = fopen( "save.txt", "r+" ); fread(Country,sizeof(struct _KeepStruct),66,stream); fread(Knight,sizeof(struct _KnightStruct),54,stream); fread(Castle,sizeof(struct _CastleStruct),8,stream); fclose(stream); When I look at the text file it seems to save the info OK, and it reads the first structure back perfectly but every other structure is screwed. I even tried messing with using sizeof() and fgetpos() and fseek() but still could not get the stupid structures to load properly. I was thinking of using individual file names but I have about 20 structures and with say 10 save games thats like 200 files, gotta be an easier way. Any info would be greatly appreciated, thanks. Rob rhuala@yahoo.com
Advertisement
....help
try this

  stream = fopen("save.bin", "b");// for each keep structure, do thisfwrite(&aKeep, sizeof(aKeep), 1, stream);// for each Kinght, etc do same thing...// read backstream = fopen("save.bin", b");fread(&aKeep, sizeof(aKeep), 1, stream);  


That should do it

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)

stream = fopen( "save.txt", "r+" );

+b for binary or +t for text. I''d use binary.

,Jay
I think that should be "wb" on the writting part and "rb" on the reading part
The rb, wb worked perfectly, thanks all...

This topic is closed to new replies.

Advertisement