mardi 9 juillet 2019

How can I save the data from a vector of structs and load it on program start?

I have given this problem a great attempt. After almost 6 hours of trying to get this working I have resorted to the internet and StackOverflow to be told my question is irrelevant and a duplicate, but here I am anyways trying to learn :)

Question: How can I save the data in my vector array (struct array) and load this data into the same array on program startup?

Description: I have been tasked with creating a program that will read data from a file. The task involves writing a program that uses a structure to store info in a file. I want to take the user entered info in my vector of structs and save it to a .txt file. The, when I open up my program again I want to load said data into the same array which is now empty due to the program just starting and continue where I left off.

Because my code is about 300 lines I have provided a link to pastebin instead.

https://pastebin.com/YBuhJCRu

Below is my attempt at saving the data from the vector to a file:

        ofstream output;
        output.open("save.txt", ios::out | ios::binary);

        for(int h = 0; h < recordkeeper.size(); h++) {

            output.write(reinterpret_cast<char *> (&recordkeeper[h]), sizeof(struct inventory));

        }

        output.close();

Aucun commentaire:

Enregistrer un commentaire