#Reading and writing struct to file for saving and loading functionality of my pokemon simulator

23 messages · Page 1 of 1 (latest)

full ore
#

I have a game loop main :

here is my file.c :

I load the player from the file to begin with, if the file doesn't exist, the program executes the initialization functions on the player "get_player()" How would I go about loading a player and saving them into a bin file? ive tried everything but after the first run the program crashes, it seems the only data that is not garbage in the file is the id and xp, for testing purposes in the file.c I initialized the loaded_player pokedex and ball inventory to NULL

tired lakeBOT
#

When your question is answered use !solved to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question use !howto ask.

crude marsh
#

what does the Player struct look like? does it contain any pointers or complex data members?
you can't do stuff like this if that's the case:

fwrite(player, sizeof(Player), 1, file);
fread(loaded_player, sizeof(Player), 1, file);

you can only really serialize an object like that if it only includes POD members. anything with a pointer in it will never make sense to flatten to a file with this type of approach since you're just going to write the address of the pointer to the file, which isn't going to be much help when you want to read it back in. you also have to be aware of things like byte alignment / endianness depending on how cross platform you want this type of thing to be.

#

if there are pointers or in the data you're trying to serialize to disk, you can work around it by creating a struct that only has POD data types in it to store the state of whatever you want to save/load, then just copy the info from the player object into that and read/write that to disk as an intermediate format rather than just reading/writing the raw Player object. and if you want to bypass all of the potential byte alignment/endianness issue you may run into on different platforms, a common approach is to just store the data ina flexible/structured text format like json (which is also typed to some extent when it comes to the essentials - int, float, string, array, object). you can compress/encrypt the file if you don't want players to access the save data then uncompress/decrypt when you read it back in.

full ore
#
typedef struct Player {
     int xp;
     char* username;
     char* showcase;
     struct PokeNode* Phead;
     struct BallNode* Bhead;
} Player;
#

@crude marsh thats what the player struct looks like, it holds two linked lists, one for the pokemons the player has (can be NULL) and one for the pokeballs the player has (can also be NULL)

#

the json approach sounds like a good idea but i really dont know what approach to take

crude marsh
#

there are some really nice/conveneient json libraries out there you could use to make this super fast/easy if you are open to using 3rd party libraries.

full ore
#

Im writing in pure C using mainly standard libraries but if there are json libraries i will definitely look into them

crude marsh
full ore
#

Im a noob at c and lower level programming in general so the main goal was to learn how to do it before abstracting it away with external libraries

crude marsh
full ore
#

Interesting ill take a look at that, and these can be used in C ? Or c++

crude marsh
#

both are C++, so if you're not compiling with a C++ compiler both of these just won't work

full ore
#

Ahh very nice thats what im looking for, thank you, ill be trying to implement this one today, i just couldnt wrap my head around dealing with complex structs in file i/o using just plain C

tired lakeBOT
#

@full ore Has your question been resolved? If so, type !solved :)

full ore
#

!solved

tired lakeBOT
#

Thank you and let us know if you have any more questions!

This thread is now set to auto-hide after an hour of inactivity

crude marsh
full ore
#

Ahh i think ove seen that one being mentioned somewhere

#

Thank you for the reference