There is such a structure:

struct account { string login; string pass; bool admin; }; 

And such a function:

 bool auth() { account user; account input; ifstream fin("users.bin", ios::binary | ios::in); if (fin.is_open()) { while (true) { system("cls"); cout << "login: "; getline(cin, input.login); cout << "pass: "; getline(cin, input.pass); while (!fin.eof()) { fin.read((char*)&user, sizeof(user)); if (input.login == user.login && input.pass == user.pass) { fin.close(); system("cls"); cout << "hello, " << user.login << "\n" << endl; cout << "press any key..."; _getwch(); return user.admin; } } system("cls"); cout << "Incorrect login or password\n" << endl; cout << "press any key..."; _getwch(); fin.clear(); fin.seekg(0, ios::beg); } } else { cout << "File is not avalible" << endl; abort(); } } 

When the function is terminated, the user object apparently cannot be removed, and an exception is raised read access violation

    1 answer 1

     fin.read((char*)&user, sizeof(user)); 

    So - just in memory - you can only read what is called POD - plain old data. The same string is a complex class that stores pointers to dynamically allocated memory, etc. things. You considered some kind of pointer, which once - in another program, perhaps - pointed to some correct, correctly allocated place in memory. And now where is he pointing?
    Exactly. Fuck knows where.
    Therefore, it turns out hell knows that when the program tries to correctly free up memory - it is not clear what, it is not clear where.

    Categorically it is impossible to keep serious things like this. The same string , if you need binary storage, it is better to write as an integer - the number of characters, and then the characters themselves. And then, when you read, first read the length, prepare the corresponding buffer, read the string into it and collect the string object.

    • one
      + Do not forget about the architecture (size of types), alignment and other things that may not be the same as on the reading side. - PinkTux
    • Well, or just replace the array of charms - lisovskey
    • This please :) - Harry
    • @lisovskey, in this case, you will either have to abandon the binary format and limit the length of the string. Either still, store the length of the string and allocate memory for the string elements of the structure dynamically. - PinkTux
    • @PinkTux Why? If you declare, say, a struct account { char login[20]; char pass[20]; bool admin; }; struct account { char login[20]; char pass[20]; bool admin; }; then everything will work fine. - Harry