Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- struct Student
- {
- char fullName[N];
- int course;
- int group;
- };
- bool operator < (Student& student1, Student &student2)
- {
- return student1.group < student2.group;
- }
- // /*
- void addRecordToBack()
- {
- Student student;
- int n = 0;
- cout << "Введите кол-во новых записей: "; cin >> n;
- fstream file("student.bin", ios::binary | ios::out | ios::app);
- cin.clear();
- while (cin.get() != '\n');
- for (int i = 0; i < n; i++)
- {
- while (cin.get() != '\n');
- cin.clear();
- cout << "фио "; cin.getline(student.fullName, N, '\n');
- cout << "курс: "; cin >> student.course;
- cout << "группа "; cin >> student.group;
- file.write(reinterpret_cast<char*>(&student), sizeof(student));
- }
- file.close();
- }
- //*/
- int numberOfRecords()
- {
- Student item;
- ifstream file;
- file.open("student.bin");
- file.seekg(0, ios::end);
- int result = file.tellg() / sizeof(item);
- file.close();
- return result;
- }
- vector<Student> getAllRecordsFromFile()
- {
- int countOfRecords = numberOfRecords();
- vector<Student> arrayOfRecords(countOfRecords);
- fstream file("student.bin", ios::binary | ios::in);
- for (int i = 0; i < countOfRecords; i++)
- file.read(reinterpret_cast<char*>(&arrayOfRecords[i]), sizeof(arrayOfRecords[i]));
- file.close();
- return arrayOfRecords;
- }
- int main()
- {
- vector<Student> students = getAllRecordsFromFile();
- setlocale(0, "");
- sort(students.begin(), students.end());
- for (int i = 0; i < students.size(); i++)
- {
- cout << "фио " << students[i].fullName;
- cout << "\nкурс: " << students[i].course;
- cout << "\nгруппа " << students[i].group;
- cout << endl;
- }
- fstream file("student1.bin", ios::binary | ios::out);
- file.close();
- file.open("student1.bin", ios::binary | ios::out);
- for (int i = 0; i < students.size(); i++)
- file.write(reinterpret_cast<char*>(&students[i]), sizeof(students[i]));
- file.close();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement