#include <iostream>
#include <vector>
#include <string>
#include <windows.h>
#include <iomanip>
#include <ctime>


struct Employee {
    std::string fullName;
    int departmentNumber;
    std::string position;
    int startYear;
};


int calculateYearsWorked(int startYear) {
    time_t t = time(nullptr);
    tm* currentTime = localtime(&t);
    return (currentTime->tm_year + 1900) - startYear;
}


std::wstring utf8_to_wstring(const std::string& str) {
    int size_needed = MultiByteToWideChar(CP_UTF8, 0, str.c_str(), (int)str.size(), NULL, 0);
    std::wstring wstr(size_needed, 0);
    MultiByteToWideChar(CP_UTF8, 0, str.c_str(), (int)str.size(), &wstr[0], size_needed);
    return wstr;
}


std::string wstring_to_utf8(const std::wstring& wstr) {
    int size_needed = WideCharToMultiByte(CP_UTF8, 0, wstr.c_str(), (int)wstr.size(), NULL, 0, NULL, NULL);
    std::string str(size_needed, 0);
    WideCharToMultiByte(CP_UTF8, 0, wstr.c_str(), (int)wstr.size(), &str[0], size_needed, NULL, NULL);
    return str;
}


std::vector<Employee> readFromFile(const std::string& filename) {
    std::vector<Employee> employees;

    HANDLE hFile = CreateFileA(
        filename.c_str(),
        GENERIC_READ,
        FILE_SHARE_READ,
        NULL,
        OPEN_EXISTING,
        FILE_ATTRIBUTE_NORMAL,
        NULL
    );

    if (hFile == INVALID_HANDLE_VALUE) {
        std::cerr << "Ошибка открытия файла!" << std::endl;
        return employees;
    }

    
    char buffer[1024];
    DWORD bytesRead;

    if (ReadFile(hFile, buffer, sizeof(buffer) - 1, &bytesRead, NULL)) {
        buffer[bytesRead] = '\0';
        std::string fileContent(buffer);

        std::wstring wideContent = utf8_to_wstring(fileContent);
        std::wstring::size_type pos = 0;

        while ((pos = wideContent.find(L'\n')) != std::wstring::npos) {
            std::wstring line = wideContent.substr(0, pos);
            wideContent.erase(0, pos + 1);

            Employee emp;
            std::wstring::size_type delimPos = 0;

            delimPos = line.find(L'|');
            emp.fullName = wstring_to_utf8(line.substr(0, delimPos));
            line.erase(0, delimPos + 1);

            delimPos = line.find(L'|');
            emp.departmentNumber = std::stoi(line.substr(0, delimPos));
            line.erase(0, delimPos + 1);

            delimPos = line.find(L'|');
            emp.position = wstring_to_utf8(line.substr(0, delimPos));
            line.erase(0, delimPos + 1);

            emp.startYear = std::stoi(line);

            employees.push_back(emp);
        }
    } else {
        std::cerr << "Ошибка чтения файла!" << std::endl;
    }

    CloseHandle(hFile);
    return employees;
}


void filterEmployees(const std::vector<Employee>& employees, int departmentNumber) {
    std::cout << "?????? ??????????? ?????? " << departmentNumber << ", ????????????? ????? 20 ???:\n";
    std::cout << std::setw(20) << "???" << std::setw(10) << "?????" << std::setw(20) << "?????????" << std::setw(15) << "????" << "\n";
    std::cout << std::string(65, '-') << "\n";

    for (const auto& employee : employees) {
        int yearsWorked = calculateYearsWorked(employee.startYear);
        if (employee.departmentNumber == departmentNumber && yearsWorked > 20) {
            std::cout << std::setw(20) << employee.fullName
                      << std::setw(10) << employee.departmentNumber
                      << std::setw(20) << employee.position
                      << std::setw(15) << yearsWorked << "\n";
        }
    }
}

int main() {
    // ?????????? ????????? UTF-8 ??? ???????
    SetConsoleOutputCP(CP_UTF8);
    SetConsoleCP(CP_UTF8);

    std::string filename = "employees.txt";

    // ?????? ?????? ?? ?????
    std::vector<Employee> employees = readFromFile(filename);

    if (employees.empty()) {
        std::cerr << "??? ?????? ??? ?????????." << std::endl;
        return 1;
    }

    // ???? ?? ????????????
    int departmentNumber;
    std::cout << "??????? ????? ??????: ";
    std::cin >> departmentNumber;

    // ?????????? ???????????
    filterEmployees(employees, departmentNumber);

    system("pause");
    return 0;
}Объясни код