Ошибка с _Pnext в цикле

Пишу код для создания, чтения и изменения бинарного файла с данными. При запуске работает всё замечательно, даже меняет все поля как в структуре, так и в файле. Однако при втором вызове void'а readEmployeeDataспосле вывода всех данных и выхода из цикла выдаёт ошибку, связанную с чтением невыделенной памяти (нарушение доступа для чтения. _Pnext было 0xFFFFFFFFFFFFFFFF). Как пофиксить?

#include <fstream>
#include <iostream>

using namespace std;

const string Surnames[] = { "Ivanov",  "Petrov",  "Kuznetsov", "Sikorsky", "Popov", "Ivankov", "Yampolsky", "Nikolayev", "Johnson", "Smith" };
const string Names[] = { "Petr", "Vasiliy", "Genadiy", "Alexander", "Igor", "Vladimir", "Alexey",  "Sergey",  "Saul", "Lev" };
const int BirthYears[] = { 1980, 1985, 1990, 1995, 2000, 2001, 2002, 2003, 2004, 2005 };
const string Degrees[] = { "Secondary", "Higher", "Candidate of Sciences", "Doctor of Sciences" };

struct Employee {
    string Surname;
    string Name;
    int birthYear;
    string Degree;
};

const int numEmployees = 10;

void writeEmployeeData(const string& fileName, Employee employees[]) {
    ofstream file(fileName, ios::out | ios::binary);
    if (file.is_open()) {
        for (int i = 0; i < numEmployees; i++) {
            file.write(reinterpret_cast<const char*>(&employees[i]), sizeof(Employee));
        }
        file.close();
    }
    else {
        cout << "Unable to open file for writing." << endl;
    }
}

void readEmployeeData(const string& fileName) {
    ifstream file(fileName, ios::in | ios::binary);
    if (file.is_open()) {
        Employee employee;
        while (file.read(reinterpret_cast<char*>(&employee), sizeof(Employee))) {
            cout << "Surname: " << employee.Surname << endl;
            cout << "Name: " << employee.Name << endl;
            cout << "Year of Birth: " << employee.birthYear << endl;
            cout << "Degree: " << employee.Degree << endl;
            cout << "---------------------------------" << endl;
        }
    }
    else {
        cout << "Unable to open file for reading." << endl;
    }
    file.close();
}

void Field_Changer(Employee& employee) {
    cout << "What would you like to change?" << endl;
    cout << "1. Name" << endl << "2. Birth Year" << endl << "3. Degree" << endl << "0. Nothing" << endl;
    int choice;
    cin >> choice;
    switch (choice) {
    case 1:
        cout << "Enter new name: ";
        cin >> employee.Name;
        break;
    case 2:
        cout << "Enter new birth year: ";
        cin >> employee.birthYear;
        break;
    case 3:
        cout << "Enter new degree:";
        cin >> employee.Degree;
        break;
    case 0:
        break;
    }
}

void searchEmployeeBySurname(const string& fileName,
    const string& searchSurname) {
    fstream file(fileName, ios::in | ios::out | ios::binary);
    if (file.is_open()) {
        Employee employee;
        bool found = false;
        while (file.read(reinterpret_cast<char*>(&employee), sizeof(Employee))) {
            if (employee.Surname == searchSurname) {
                found = true;
                cout << "Employee with surname " << searchSurname << " found." << endl;
                cout << "Surname: " << employee.Surname << endl;
                cout << "Name: " << employee.Name << endl;
                cout << "Year of Birth: " << employee.birthYear << endl;
                cout << "Degree: " << employee.Degree << endl;

                Field_Changer(employee);
                int employeeIndex = file.tellg() / sizeof(Employee) - 1;
                file.seekp(employeeIndex * sizeof(Employee));
                file.write(reinterpret_cast<const char*>(&employee), sizeof(Employee));
                break;
            }
        }
        if (!found) {
            cout << "Employee with surname " << searchSurname << " not found."
                << endl;
        }
        file.close();
    }
    else {
        cout << "Unable to open file for searching and updating." << endl;
    }
}

int main() {
    Employee employees[numEmployees];

    for (int i = 0; i < numEmployees; ++i) {
        employees[i].Surname = Surnames[i];
        employees[i].Name = Names[i];
        employees[i].birthYear = BirthYears[i];
        employees[i].Degree = Degrees[rand() % 4];
    }

    writeEmployeeData("employees.bin", employees);

    readEmployeeData("employees.bin");

    cout << "Enter the surname to search for: ";
    string searchSurname;
    cin >> searchSurname;
    cout << endl;

    searchEmployeeBySurname("employees.bin", searchSurname);

    readEmployeeData("employees.bin");

    return 0;
}

Ответы (0 шт):