0

So i have this binary file with some player's data written in it. I want to print the hole file, ( i have class player that contains some data) there is some data printed to the screen but after that an exception is thrown.

void Player::print_statistics()
{
    ifstream file; string str; Player plyr;
    file.open("player_database.dat", ios::binary);
    while(file.read((char*)&plyr, sizeof(Player)))
    {
        file.read((char*)&plyr, sizeof(Player));
        cout << plyr.full_name << "   " << plyr.ingame_name << "   " << plyr.age << "   " << plyr.hours_played << "\n";

    }
}

Exception thrown: read access violation. _Pnext was 0x148E2B4. occurred. I tried the following:

while(!file.eof())

doesn't work

while(file>> string) 

doesn't work. This is the calss :

using namespace std;
class Player 
{
protected:
    string password;
    int age = 0;
    long int id_nmber=0 ;
    string email, ingame_name, full_name;
public:
// ...
};
  • What is `Player`? Are you trying to deserialise complex types? `_Pnext` suggests a standard library type like a container or `std::string`. You can't just byte-copy those; their direct contents are often just pointers to data on the original machine... – Asteroids With Wings May 20 '20 at 12:30
  • @Asteroids With Wings Simple class contains some attributes ( like name/ age .. ). – Yassine Mrabet May 20 '20 at 12:31
  • 2
    Are you sure you want the `;` at the end of the `while`? Also, you are doing an unchecked `read` within the body of the `while` as well. – cigien May 20 '20 at 12:31
  • @YassineMrabet -- You may think the class is simple, but it may not be. Please post the class. – PaulMcKenzie May 20 '20 at 12:32
  • As suspected, your code will not work for `Player`. First, what is the last parameter to `read`? It is `sizeof(Player)`, and that cannot work. The `sizeof` is a compile-time value, thus it cannot change. What if one of those strings contained 1000 characters? How would `sizeof` figure out there are 1000+ characters to write to the file? – PaulMcKenzie May 20 '20 at 12:34
  • There we go; called it. :) – Asteroids With Wings May 20 '20 at 12:35
  • Whatever you're trying to do, drop this immediately -> `file.read((char*)&plyr, sizeof(Player))`. You're writing raw binary data to a non-POD type. Results of such operation are undefined. – Big Temp May 20 '20 at 12:35
  • https://stackoverflow.com/q/55942970/4386278 too – Asteroids With Wings May 20 '20 at 12:36
  • @YassineMrabet [Check this](http://coliru.stacked-crooked.com/a/fa071a40e6161fca). That answer has to return "Yes" if that class can be used to read to and write to a file in binary fashion. As you can see, the answer returned from querying whether `Player` is trivially copyable is "No". If you want to verify this, here is the [same class with the `string` members removed](http://coliru.stacked-crooked.com/a/deba30301518aa8e). Now the answer is "Yes": – PaulMcKenzie May 20 '20 at 12:40
  • @PaulMcKenzie "No" it isn't .. So i guess i should change everything to text file because i need this string type. – Yassine Mrabet May 20 '20 at 12:43
  • Use simple `char` arrays instead of `std::string`. That would be one way. Or keep the class as it is now and properly serialize the data. – PaulMcKenzie May 20 '20 at 12:45
  • @PaulMcKenzie So i changed every string to a char*, will this function work ? – Yassine Mrabet May 20 '20 at 13:01
  • No it will not work if you just change to a pointer. A pointer is not an array. – PaulMcKenzie May 20 '20 at 13:01
  • @ PaulMcKenzie what can i do then ? – Yassine Mrabet May 20 '20 at 13:04
  • `char name[SomeSize];` -- Wouldn't that be obvious? – PaulMcKenzie May 20 '20 at 13:05

0 Answers0