I need to save an object to a file and then reload it later on. I think this is called serialization but I don't know. I have made a simple example below that I need to fix.

#include <iostream>
#include <fstream>
#include <windows.h>

using namespace std;

#define MAXV 1000

class edgenode {
public:
	int y;
	int weight;
	edgenode *next;
};

class graph {
public:
	edgenode *edges[MAXV+1];
	int degree[MAXV+1];
	int nvertices;
	int nedges;
	int directed;
};

int main(){
	graph *g[2];

	g[1] = new graph();

	g[0] = new graph(); // Initialize Graph
	g[0] -> nvertices = 2;

	ofstream ofile;
	ofile.open("graph.txt", ios::binary);
	ofile.write((char*)&g[0], sizeof(g[0]));

	ifstream ifile;
	ifile.open("graph.txt", ios::binary);
	ifile.read((char*)&g[1], sizeof(g[1]));

	cout << g[0] -> nvertices << endl;
	cout << g[1] -> nvertices << endl;

	system("pause");
	
	return 0;
}

Basically I create a graph, save it, and then load it into another graph. Then the member variable for both graphs should be 2. However, the first graph is two and the loaded graph is always 0. How do I fix this? I need the second graph to exactly match the first graph.

BTW, I can't simply store the information in a text file. For example, I can't simple get the value of each variable manually and write it to the file and then load those values later and set them as the values for the new graph. The problem with that is the array of linked lists, its going to be hard to record the parse the information, so I would rather just store the exact object and reload the exact object.

>>line 35: ofile.write((char*)&g[0], sizeof(g[0]));

sincfe g[0] is a pointer, sizeof(g[0]) is the same as sizeof(any pointer) which is normally 4 with a 32-bit compiler. What you want there is sizeof(graph). But that won't really do what you want either because it doesn't include the edgenode array other than that array of pointers.

The next thing wrong with that line is that g[0] has been allocated memory but otherwise uninitialized. class graph does not have a constructor so none of its data variables will be initialized to anything other than whatever is in the memory at the time memory location is allocated.

Considering the above, it is no surprise that the read part of that function just reads a lot of crap. Garbage out, garbage in.

why does class graph contain an array of edgenode pointers? Is there going to be MAXV number of linked lists? If on the otherhand there is only going to be one linked list then that class doesn't need more than one edgenode pointer, which will be the head of the linked list.

If you want to serialize that linked list(s) then you have to wrote out each node individually -- there is no way to write them all at the same time.

I changed it to sizeof(graph)+sizeof(edgenode) and it works :)

One question though, it only works if I write it like this,
file.write((char*)&g, sizeof(graph)+sizeof(edgenode));
file.read((char*)&g, sizeof(g));

and if I change the read function to
file.read((char*)&g, sizeof(graph)+sizeof(edgenode));

I get an error. Why is that? Shouldn't it be the same size at what's being written?

sizeof(edgenode) has the same problem as sizeof(g[0]) -- edgenode is an array of pointers. It makes no sense to save pointers because those pointers will not be valid when read back into the program. As I said before, it is not possible to save a linked list in one shot -- each node must be saved.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.