I'm still stuck. I had' a Look at boost, but had to conclude that this is way more advanced stuff than what I'm used to so far.
I also continued to search the Internet for solutions but found nothing that I understood or/and could adapt to my specific case.
Further, I experimented a bit with the code I already have, which gave me a few new insights:
- Writing one instance from the vector to file and reading it back works as already stated.
- I can then create more instances, store them in the vector and access all without problem. But still, once I end the program I can then only load back one from the file. I don't know if the rest are even saved as reading the file the way I do it at the moment only ever loads the first instance.
- The content of the player inventory (the vector in line 15) does not seem to get saved along with the rest as I cannot access it on the loaded instance.
So the problem persists and there is a new one on top.
Since the first instance stored in the vector is saved and loaded without problem, I conclude that probably all the other instances get saved as well. After all, I append them manually to the end of the file, so they should be there. So the only problem that remains is how to read them back one by one past the first one. Putting them back into the vector should then be easy.
If possible I'd like to avoid writing and reading every data member individually as the class is quite large and has a lot of members (and new ones might get added later). Currently it stands at 40 variables, 7 functions and 4 vectors. If there is a possibility to just store and read the whole thing as one package, that would be very much preferred ;-)
Why does the inventory (vector from line 15) not work after loading the instance of the player-class back from file? Any ideas? Is it because it is a vector as well? Do I have to save it separately?
EDIT:
If I know the size of the file and divide that through the size of one instance, I get the number of stored instances. Can I do something with that? Basically, tell the program to read one entry, then jump so many bytes ahead, read the next, then again jump, read the next an so on until number of entries reached?
I use the following code to get these values:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
|
std::ifstream Input("PlayerList.pl", std::ios::binary);
Input.seekg(0, std::ios::end);
int FileSize = static_cast<int>(Input.tellg());
std::cout << "\n Size of the file is " << FileSize << "bytes\n";
std::cout << " The size of Player is " << sizeof(Player) << " bytes\n";
int NumberPlayers = FileSize / sizeof(Player);
std::cout << " The Number of stored Players are: " << NumberPlayers << std::endl;
|
Playing around with it I already confirmed that all instances are saved as the file get's larger by the size of one instance whenever a new one is appended to it.