Загрузка данных


#include <iostream>
#include <string>
#include <cstdlib>   // для rand() и srand()
#include <ctime>     // для time()
#include <cstring>   // для strcpy

using namespace std;

struct Students
{
    char fio[12];
    char name[10];
    int year;
    int month;
    int date;
    int grade[4];
};

// Функция для ручного ввода одного студента
void inputManual(Students &a, int index) {
    cout << "\n--- Студент " << index + 1 << " ---\n";
    cout << "FIO students: ";
    cin >> a.fio;
    cout << "Name students: ";
    cin >> a.name;
    cout << "Year of birth: ";
    cin >> a.year;
    cout << "Month of birth: ";
    cin >> a.month;
    cout << "Date of birth: ";
    cin >> a.date;          // было a.year — исправлено!
    cout << "Assessment in mathematics: ";
    cin >> a.grade[0];
    cout << "Assessment by history: ";
    cin >> a.grade[1];
    cout << "Computer science assessment: ";
    cin >> a.grade[2];
    cout << "Assessment in physics: ";
    cin >> a.grade[3];
}

// Функция для генерации случайных данных одного студента
void inputRandom(Students &a, int index) {
    // Списки для случайного выбора
    const char* fios[] = {"Ivanov", "Petrov", "Sidorov", "Kuznetsov", "Smirnov"};
    const char* names[] = {"Ivan", "Peter", "Alex", "Dmitry", "Maxim"};

    strcpy(a.fio, fios[rand() % 5]);
    strcpy(a.name, names[rand() % 5]);
    
    a.year = 1990 + rand() % 15;      // 1990–2004
    a.month = 1 + rand() % 12;        // 1–12
    a.date = 1 + rand() % 28;         // 1–28 (упрощённо, без проверки месяца)
    
    for (int i = 0; i < 4; i++) {
        a.grade[i] = 2 + rand() % 5;  // оценки 2,3,4,5,6
    }
}

int main() {
    FILE *fp;
    int n, choice;
    Students a;
    
    srand(time(NULL)); // инициализация генератора случайных чисел

    if ((fp = fopen("student.dat", "w+")) != NULL) {
        cout << "Kol-vo students: ";
        cin >> n;

        cout << "\nChoose input method:\n";
        cout << "1 — Enter manually\n";
        cout << "2 — Generate randomly\n";
        cout << "Your choice: ";
        cin >> choice;

        for (int i = 0; i < n; i++) {
            if (choice == 1) {
                inputManual(a, i);
            } 
            else if (choice == 2) {
                inputRandom(a, i);
                // Выведем сгенерированные данные для контроля
                cout << "\n--- Student " << i + 1 << " (random) ---\n";
                cout << "FIO: " << a.fio << ", Name: " << a.name 
                     << ", Born: " << a.year << "." << a.month << "." << a.date << "\n";
                cout << "Grades: " << a.grade[0] << " " << a.grade[1] 
                     << " " << a.grade[2] << " " << a.grade[3] << "\n";
            }
            else {
                cout << "Wrong choice! Exiting.\n";
                break;
            }
            
            fwrite(&a, sizeof(Students), 1, fp); // было write → fwrite
        }
        
        cout << "\nInput completed. Data saved to student.dat\n";
        fclose(fp);
    } 
    else {
        cout << "Error opening file!\n";
    }

    return 0;
}