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

  • 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 [mre], and if making the sucker doesn't end with you spotting and fixing the bug, add the MRE to the question. – user4581301 Nov 26 '20 at 23:35
  • Notre: only use `#include ` when you know what you are doing ([and at that point you probably won't be using it](https://stackoverflow.com/questions/31816095/why-should-i-not-include-bits-stdc-h)). The way you're currently using it suggests you don't know how it should be used. – user4581301 Nov 26 '20 at 23:37
  • `clientes2 = new list[2];` is not a `list` with two items in it. It's an array of two `list`s with **0** items in them. What you want to do is make a `list`: `list 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](https://stackoverflow.com/questions/12340402/reading-a-string-from-a-file-in-c) with `read`. It's also too complicated to `write`. – user4581301 Nov 26 '20 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 want[`std::vector`](https://en.cppreference.com/w/cpp/container/vector) instead. – user4581301 Nov 26 '20 at 23:48
  • I think that what you need right now [is a good C++ textbook](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). – Sam Varshavchik Nov 26 '20 at 23:56

0 Answers0