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.