I am studying reading/writing to binary files with C ++, i'm trying to apply with object lists,
class Customer {
public:
int code;
string name;
void ini(int code, string name);
}
list<Customer> *customers;
customers = new list<Customer>[2];
Customer *cl = new Customer();
cl->ini(17, "John");
customers[0].push_back(*cl);
Writing method:
ofstream wf("customers.dat", ios::out | ios::binary);
for(int i = 0; i < 2; i++) {
wf.write((char *) &customers[i], sizeof(Customer));
cout << i << endl;
}
wf.close();
And reading method:
list<Customer> *customer2;
customer2= new list<Customer>[2];
for(int i = 0; i < 2; i++) {
rf.read((char *) &customer2[i], sizeof(Customer)); //some way of using the push_back by here?
}
rf.close();
after debugging, the recording seems to go well, however when I do the reading, only the first item of the first vector is loaded, the loop cannot find an end and the program stops.
for(int i=0; i < 2; i++) {
cout << i << " ";
for (auto x : customers[i])// here fails
cout << x->name << endl; //print the first member and break
}
what is the correct way to manipulate object vector lists?
P.S: For the sizeof, i've already tried: (customers, Customer, customer[i], customer2[i])