0

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])

The entire code

5
  • An etiquette note: All information required to understand the question and any answers it received must be in the question. Links aren't good enough. Links rot. Tomorrow that code link may be gone. Might point to something not safe for work. Might still be there, but blocked by a firewall. What you really want to to is fashion a minimal reproducible example, and if making the sucker doesn't end with you spotting and fixing the bug, add the MRE to the question. Commented Nov 26, 2020 at 23:35
  • Notre: only use #include<bits/stdc++.h> when you know what you are doing (and at that point you probably won't be using it). The way you're currently using it suggests you don't know how it should be used. Commented Nov 26, 2020 at 23:37
  • clientes2 = new list<Customer>[2]; is not a list with two items in it. It's an array of two lists with 0 items in them. What you want to do is make a list: list<Customer> clientes2;. then make a Customer temp; and then read into temp with rf.read((char *) &temp, sizeof(temp));. then add temp to the clientes2: clientes2.push_nack(ttemp);. BUT!!!!!! Customer` contains a string and string is too complicated to read from a file with read. It's also too complicated to write. Commented Nov 26, 2020 at 23:46
  • also worth noting that list in C++ is almost always a doubly linked list. If you're coming in from a language where list is a dynamic array, you wantstd::vector instead. Commented Nov 26, 2020 at 23:48
  • I think that what you need right now is a good C++ textbook. Commented Nov 26, 2020 at 23:56

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.