Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

(C++) Writing and reading a dynamic array to a binary file

I’m new here so forgive me if I make some mistakes.
Anyway, I’ve got this code here:

class List{
    private:
        int dim;
        Reservations *res; //Dynamic array of reservations that composes "List"
    public:
        List();
        List(int dm, Reservations *resv);
        ~List();
        int getDim();
        void setDim(int dm);
        void readNwrite(List &ls);
};

This is a class and I need to write and read the Reservations array in a binary file. The Reservations class is composed of other types of data (two strings, an integer and also another class).
Here you can see the Reservations class:

class Reservations{
    private:
        DateTime dt;
        string name, phone;
        int codVisit;
    public:
        Reservations();
        Reservations(DateTime datT, string nm, string tel, int cod);
        ~Reservations();
        DateTime getDt();
        void setDt(DateTime datT);
        string getName();
        void setName(string nm);
        string getTel();
        void setTel(string tel);
        int getCodVisit();
        void setCodVisit(int cod);
        void read();
        void print();
};

And here’s the class DateTime:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

class DateTime{
    private:
        Date d;
        int sec, min, hrs;
    public:
        DateTime();
        DateTime(int s, int m, int o, int d, int ms, int y);
        ~DateTime();
        void getDt();
        void setDt(int g, int ms, int y);
        int getSec();
        void setSec(int s);
        int getMin();
        void getMin(int m);
        int getOre();
        void getOre(int o);
        void print();
        void read();
        int validate();
        int compare(DateTime dt1);
    friend void Anglform(DateTime dt);
};

This is how I’ve done created and read the binary file in the List class:

void List::readNwrite(List &ls){
    ofstream file("list.dat", ios::binary);
    file.write(reinterpret_cast<char*>(res), sizeof(Reservations) * dim);
    file.close();

    ifstream fileF("list.dat", ios::binary);
    ls.setDim(dim);
    ls.res = new Reservations[ls.dim];
    fileF.read(reinterpret_cast<char*>(ls.res), sizeof(Reservations) * dim);
    file.close();
}

I’ve tried it but it isn’t working. I know that the second instance is getting the contents of the first one, but in the end the program always crashes…

>Solution :

To be able to read/write a structure to/from a file as a binary blob, that structure must be standard layout type (aka POD) and not contain any pointers within. Your structure obviously does not satisfy that requirement (std::string object is not a standard layout type and does contain a pointer(s) inside) so you have to write reading/writing methods to load/store data member by member or use a library designed for that purpose.

Note: your class Reservations violates the rule of 3/5/0
and you have memory leak right there on this line:

ls.res = new Reservations[ls.dim];

You better use std::vector for dynamic array or if you cannot at least a smart pointer.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading