Dump object into file | Restore object from file later

So I've messed around with XML and boost:serialization (which seems to be gone in the latest version btw) and God knows what.

I've spent so long trying to figure out this that I have decided that I really just need to ask someone. How can I do this? I have a class, like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <vector>
#include "Map.h"
#include "NPC.h"

using std::vector;

class Database
{

public:
vector<Map> maps;
vector<NPC> npcs;

Database()
{

// Populate the vectors and do initial stuff

}

};


Then elsewhere I do:

1
2
3
4
5
6
7
8
Database database;

// Let the game play for a while
do_stuff();
do_more_stuff();

// The database's members have now changed, time to save it into a file.
// This is where I have no idea what to do. 


And additionally I need to be able to load this into an object again. SOmething like this?
1
2
Database database;
// magic function to make the new object equal to the one saved from earlier 
Last edited on
Well, you need to put all of the objects data members into the file in such a way that they can be accurately reloaded. Here's a simple method:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
void save(const Object& obj)
{
    ofstream file("save.txt");

    file << ":x:" << obj.x << endl;
    file << ":y:" << obj.y << endl;
    file << ":end:" << endl;
}

void load(Object& obj)
{
    int tx, ty;
    ifstream file;
    string line;

    file.open("save.txt");

    while (file.good())
    {
        getline(file, line, ':');


        if (line=="x")
        file >> tx;
        if (line=="y")
        file >> ty;

        if (line=="end")
        {
            obj.init(tx, ty);
            break;
        }
    }
}


That's probably not the most efficient way for storing object data, but it works.
Thanks you so much! But where do I put this code? Inside the class or elsewhere? Also, do I need to modify it to fit my class or is it plug'n'play?
It will obviously need to be modified to save all your classes members. You will probably want to make it a global function, so all instances of your class can be saved at once, rather than one at a time (if it were a member function). Also, I would suggest that you add error checking to these functions. (file.good(), file.is_open(), etc)
So here:
file << ":x:" << obj.x << endl;

I change this to real attributes and copy?

Also, the class has some vector members, is that possible?
To your first question, yes. To your second, kindof. You will need to find a way to store all the elements of the vector, and then reload them. This can be done in a way similar to the first method I gave you.
Perhaps with several files? Into a folder?
Noooo, you would just implement a way of storing the vectors elements into the same file.

Try adding this into the loop that parses the file:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
if (line=="intvec")
{
    while (true)
    {
        getline(file, line, ':');
 
        if (line=="element")
        {
            file >> tempInmt;
            vectorOfInts.push_back(tempInt);
        }
        if (line=="end")
        break;
    }
}
Topic archived. No new replies allowed.