Trouble with Serialization

I'll post the relevant code and then explain the problem.

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
36
class graph {
public:
	edgenode *edges[MAXV+1]; // another class, not relevant
	int degree[MAXV+1];
	int nvertices;
	int nedges;
	int directed;
};
void saveGraph(int temp, graph *g){
	ofstream file;
	string filename;
	string str1 = "graph";
	string str2;
	string str3=".txt";
	stringstream out;
	out << temp;
	str2 = out.str();
	filename = str1 + str2 + str3;
	file.open(filename.c_str());
	file.write((char*)&g, sizeof(g));
	file.close();
}
void loadGraph(int &i, graph *g[]){ // i is an index of the number of graphs
	ifstream file;              // g[] is an array of size 10 holding all the graphs
	string filename;
	cout << "Filename: ";
	cin >> filename;
	file.open(filename.c_str());
	graph *temp;
	file.read((char*)&temp, sizeof(temp));
	temp ->nvertices;
	g[i] = temp;
	cout << g[0] -> nvertices;
	cout << g[i] -> nvertices;
	i++;
}


So basically when I run my program, I type a command to create a graph g[0] with a certain amount of vertices (nvertices). I do this for another graph g[1] and then I call saveGraph() on g[1] and it saves the object to a text file. When I call loadGraph() on the saved file for g[1] a new graph is created in the next spot, (g[2] in this case), but the members of the graph don't follow the members of g[1], the equal the members of the first graph created, or g[0]. I can't figure out why this is happening. I'm sorry if that was confusing, just ask and I'll reexplain it. Also ask if you need the entire code to look at.
You're trying to save a pointer to a file. In order for that to work, when you load the file the object would have to happen to be created at the exact same location than when you wrote the file.
If only serialization was that easy...

Graphs are not exactly the easiest of structures to serialize. First you need to store the graph in a vector, then you can go through the vector writing the details about each node to the file.
The file could look like this:
Node 1 points to nodes 2, 3, and 7
Node 2 points to nodes 1, 5, and 7
...

(You don't have to store the information to verbosely, obviously.)
Then when you load the file, you reconstruct the graph based on this information.
So you're basically saying don't store the exact object, just store all of the variables and information, then recreate a graph with all of that information? It seems like there should be an easier way to do it...

I have the details of each node stored in a linked list... I don't want to have to go through the process you described, saying "Node 1 points to nodes 2, 3, and 7". I would like to just save my linked list (which is the member edgenode *edges[] in the class graph) so I can easily reload it next time...

The method I'm using now does create a graph and it fills in all of the variables of the class. The problem is that it fills in the variables as the members of the first graph created, g[0]. Even if I save a graph with variables set to values different from g[0], when I load that saved file, it will always use the information of g[0]. Since it is atleast loading some data (just from the wrong place), there should be a way to fix it.

(This seems harder than I thought... a mod may want to move it to the intermediate section)
Last edited on
That's the way serialization works. You simply can't store memory pointers in non-memory. Or rather, you can, but they won't be usable when you retrieve them. You have to decompose the structure into its smallest parts.
I figured it out. I just changed, the write function to
file.write((char*)&g, sizeof(graph)+sizeof(edgenode));

however, I have to leave the file.read() function as sizeof(g) or it will give me an error... Why is that? It works fine now, but I'm just curious.
That... shouldn't work at all.
Topic archived. No new replies allowed.