Hello All,
I'm trying to serialize a set of structs in C++. This works great for all data except a vector contained in my struct. I can write the data to disk, and then read all data back into memory. The only problem is when I try to access an element of the vector I get a segmentation fault. My code is below. Any help is greatly appreciated.
Program to write to disk
int main {
struct Student one;
strcpy(one.FullName, "Ernestine Waller");
strcpy(one.CompleteAddress, "824 Larson Drv, Silver Spring, MD 20910");
one.Gender = 'F';
one.LivesInASingleParentHome = true;
one.grades.push_back(80);
one.grades.push_back(90);
ofstream ofs("fifthgrade.ros", ios::binary);
ofs.write((char *)&one, sizeof(one));
ofs.close();
}
Program to read from disk
int main {
struct Student *two = (struct Student *) malloc(sizeof(struct Student));
ifstream ifs("fifthgrade.ros", ios::binary);
//cout << "Size of struct: " << size << endl;
ifs.read((char *)two, sizeof(struct Student));
cout << "Student Name: " << two->FullName << endl;
cout << "First Grade: " << two->grades[0] << endl;
cout << "Second Grade: " << two->grades[1] << endl;
ifs.close();
return 0;
}