Thread: store a class

  1. #1
    Registered User
    Join Date
    Jan 2003
    Posts
    45

    store a class

    I'm trying to store a class in a file but I dont know exactly how to do that.
    This is how far I've come:

    Code:
    void saveD(Data& savedD)
    {
    	ofstream save("Data.dat",ios::trunc,filebuf::binary);
    	save<<savedD;
    	
    	if(!save.bad())
    	{
    		cout<<"Error saving data";
    		save.close();
    		exit(0);
    	}
    
    	save.close();
    }
    Of corse it doesnt work but I cant figure out what to do now...
    Oh and some code to load the data from the file would be handy too

  2. #2
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    Code:
    class OBJECT
    {
       ...
    };
    
    bool SaveObject(const char* FileName, OBJECT* Object)
    {
       ofstream WriteFile;
       WriteFile.open(FileName, ios::out | ios::binary);
    
       if(WriteFile.fail()) return false;
    
       WriteFile.write((char*)Object, sizeof(OBJECT));
    
       WriteFile.close();
       return true;
    }
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  3. #3
    Registered User
    Join Date
    Jan 2003
    Posts
    45
    thanks.. and any idea how to load the whole class again?

  4. #4
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    The reverse, using read instead of write:
    Code:
    OBJECT Object;
    ReadFile.read((char*)&Object, sizeof(OBJECT));
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  5. #5
    Registered User
    Join Date
    Jan 2003
    Posts
    45
    And that'll store all the information in 'Object' ?

  6. #6
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    Originally posted by Ivan!
    And that'll store all the information in 'Object' ?
    Yep!
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  7. #7
    Registered User
    Join Date
    Jan 2003
    Posts
    45
    Thanx I needed that!

  8. #8
    Registered User
    Join Date
    Jul 2003
    Posts
    3
    Hi, I've been trying to use this code but with no success.

    Saving

    ofstream writeFile;
    writeFile.open("save.dat", ios:ut | ios::binary);
    writeFile.write((char*)&playerTemp, sizeof(character));
    writeFile.close();

    Reading

    ifstream readFile;
    readFile.open("save.dat", ios:ut | ios::binary);
    readFile.read((char *)&playerTemp,sizeof(character));
    readFile.close();

    How does the load actually work, does it load the saved class info from file into playerTemp, overwriting what's in it already? The reason I am asking this is because playerTemp gets initialised everytime the application starts (default values). Then when I try the load function to overwrite the variable, it doesn't do anything, playerTemp retains its values that it got upon initialization. Do I need a new clean variable to store loaded information?

    I've put in a couple of test checks and they both return true, in that the saveFile and readFile functions returned no errors.

    Thanks

  9. #9
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    ifstream readFile;
    readFile.open("save.dat", ios::out | ios::binary);
    readFile.read((char *)&playerTemp,sizeof(character));
    readFile.close();
    The ios::out part above needs to be ios::in. Use out when doing output, use in when inputting.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  10. #10
    Registered User
    Join Date
    Jul 2003
    Posts
    3
    OK...I tried that but it still does not work.... Anyway, this is the code for the whole section.


    void exeCommand(int number, character playerChar, character* playerTemp, bool* gameOn)
    {
    switch (number)
    {
    case 1:
    {
    ofstream writeFile;
    writeFile.open("save.dat", ios:ut | ios::binary);

    writeFile.write((char*)&playerTemp, sizeof(character));
    writeFile.close();
    }
    break;
    case 2:
    {
    ifstream readFile;
    readFile.open("save.dat", ios::in | ios::binary);
    readFile.read((char *)&playerTemp,sizeof(character));
    readFile.close();
    }
    break;
    case 3:
    {
    showStats(playerChar);
    }
    break;
    case 4:
    {
    *gameOn = false;
    }
    break;
    default:
    {
    cout << "Unrecognisable command";
    }
    }
    }


    Basically this is what happens

    1) I start the app. I enter a name. Then an instance of character gets initialised.
    2) I have a command that returns the name, and it displays correctly (the name I entered in step 1)
    3) I make it do the SAVE function
    4) Exit application
    5) Reopen application
    6) Enter a new name
    7) Return name, and it's the new name I just entered as it should be.
    8) OK... now I make the app run the LOAD function.
    9) I do a display info, but the name is still the new one, not the one that I had saved in the first run of the app. Is this not how it's supposed to work?

  11. #11
    Registered User
    Join Date
    Jul 2003
    Posts
    3
    Oops...sorry, I missed the sticky...my apologies.

    Code:
    void exeCommand(int number, character playerChar, character* playerTemp, bool* gameOn)
    {
    	
    
    	switch (number)
    	{
    	case 1:
    		{
    			ofstream writeFile;
    			writeFile.open("save.dat", ios::out | ios::binary);
    
    			writeFile.write((char*)&playerTemp, sizeof(character));
    			writeFile.close();
    		}
    		break;
    	case 2:
    		{
    			ifstream readFile;
    			readFile.open("save.dat", ios::in | ios::binary);			
    
    			readFile.read((char *)&playerTemp,sizeof(character));
    			readFile.close();
    		}
    		break;
    	case 3:
    		{
    			showStats(playerChar);
    		}
    		break;
    	case 4:
    		{
    			*gameOn = false;
    		}
    		break;
    	default:
    		{
    			cout << "Unrecognisable command";
    		}
    	}
    }

  12. #12
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    You are reading and writing your data to/from the playerTemp variable but you are displaying stats using the playerChar variable. Is this what you want to do? If playerChar holds the new instance and you are only ever displaying the info in that instance, then it doesn't matter that you are readin/writing into this playerTemp variable. Makes me wonder if your file actually contains any usefull data. You may create something in playerChar but you try to write out info from playerTemp into the file. I can't really tell though without seeing more code... like the code which creates this instance, initializes it and then calls this exeCommand function. One of these variables is probably not needed I am guessing.

    Also, if I were you I would create some new member functions for the class called read and write and showStatsthen you could replace the relevant code in the exeCommand with:
    Code:
    case 1: playerTemp.write("save.dat");
            break;
    case 2: playerTemp.read("save.dat");
            break;
    case 3: playerTemp.showStats();
            break;
    You might not need one of the playerTemp or playerChar variables if you determine that one of them is not needed.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with FIFO QUEUE
    By jackfraust in forum C++ Programming
    Replies: 23
    Last Post: 04-03-2009, 08:17 AM
  2. Replies: 8
    Last Post: 10-02-2005, 12:27 AM
  3. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  4. structure vs class
    By sana in forum C++ Programming
    Replies: 13
    Last Post: 12-02-2002, 07:18 AM