I want to save nested structures in file. I found this code and decided to rework it to work with vectors. But unfortunately it returns me a lot of errors and I have no idea how to fix them?
#include <fstream>
#include <iostream>
#include <vector>
#include <string.h>
using namespace std;
typedef struct Type
{
int number;
};
typedef struct student
{
char name[10];
int age;
vector<Type> grades;
};
int main()
{
std::vector<student> st;
for (int i = 0; i < 3; i++)
{
student st1;
std::cin >> st1.age;
std::cin >> st1.name;
Type type;
type.number = i;
st1.grades.push_back(type);
st.push_back(st1);
}
ofstream output_file("students.data", ios::binary);
output_file.write((char*)&st, sizeof(st));
output_file.close();
ifstream input_file("students.data", ios::binary);
std::vector<student> master;
input_file.read((char*)&master, sizeof(master));
for (size_t idx = 0; idx < 3; idx++)
{
cout << "Record #" << idx << endl;
cout << "Name: " << master[idx].name << endl;
cout << "Age: " << master[idx].age << endl;
cout << "Grades: " << endl;
for (size_t i = 0; i < master[idx].grades.size(); i++)
cout << master[idx].grades[i].number << " ";
cout << endl << endl;
}
return 0;
}
Erros -
Exception thrown at 0x007CC951 Unhandled exception at 0x007CC951. Access violation writing location 0xDDDDDDDD.