Thread: Saving Struct to Binary File/Copying Structs

  1. #1
    Registered User
    Join Date
    Feb 2009
    Posts
    26

    Saving Struct to Binary File/Copying Structs

    I've tried following the instructions here, but I'm having some trouble loading the files. I think this may be because of the strings and vector I use in my structure, as I found a post while googling that suggested resizable (structures?) as a possible problem.

    Anyways, here's the structure:
    Code:
    struct _game
    {
        std::string plyr_name;
    
        std::string plyr_class;
    
        int plyr_x;
        int plyr_y;
    
        int plyr_health;
        int plyr_maxhealth;
    
        int plyr_ap;
    
        int plyr_str;
        int plyr_dex;
        int plyr_per;
        int plyr_end;
        int plyr_int;
    
        map current_map;
    
        string shortlog[6];
        vector<string> longlog;
    
        vector<enemy> enemies;
    };
    And here are the save and load functions...
    Code:
    void save_game(_game Game)
    {
        ofstream savefile("save.dat", ios::out | ios::binary);
    
        savefile.write(reinterpret_cast<char *>(&Game),sizeof(_game));
    
        savefile.close();
    }
    
    void load_game()
    {
        _game loadgame;
    
        ifstream loadfile("save.dat", ios::in | ios::binary);
    
        loadfile.read(reinterpret_cast<char *>(&loadgame),sizeof(_game));
    
        loadfile.close();
    }
    The program compiles fine, but when I try to load a game it breaks ("Your Program has encountered a problem and needs to close")... saving seems to work fine, but I'm thinking the problem is with the syntax somewhere in there, as I have no idea how most of the stuff in the read and write functions works...

    That being said, even when I do get a game loaded, how would I go about copying the information from the loaded structure to a pre-existing one used to play?

    Thank you for your time, sorry if this comes across as immature, but I'm tired, and have things I need to do before I go to sleep.

    Timothy
    My Dev Blog - The most up-to-date info on my current projects.

  2. #2
    Not stupid, just stupider yaya's Avatar
    Join Date
    May 2007
    Location
    Earthland
    Posts
    204
    Yes, the vectors and strings are a problem because their information doesn't (technically) exist in the struct. The only way to do it is to load/save each member at a time. When you get to the strings and vectors, you must then figure out how many objects there are in them (by reading a value) and then looping through them all.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help with linked list sorting function
    By Jaggid1x in forum C Programming
    Replies: 6
    Last Post: 06-02-2009, 02:14 AM
  2. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  3. hi! saving a struct..., kewl! but need more info...,
    By mickey in forum C++ Programming
    Replies: 4
    Last Post: 05-10-2002, 02:02 AM
  4. what does this mean to you?
    By pkananen in forum C++ Programming
    Replies: 8
    Last Post: 02-04-2002, 03:58 PM
  5. reading from structs in a binary file
    By Unregistered in forum C Programming
    Replies: 4
    Last Post: 12-21-2001, 10:52 AM