SkeptaProgrammer

Untitled

Jun 11th, 2019
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.91 KB | None | 0 0
  1. struct Student
  2. {
  3.     char fullName[N];
  4.     int course;
  5.     int group;
  6. };
  7.  
  8. bool operator < (Student& student1, Student &student2)
  9. {
  10.     return student1.group < student2.group;
  11. }
  12.  
  13. // /*
  14. void addRecordToBack()
  15. {
  16.     Student student;
  17.     int n = 0;
  18.     cout << "Введите кол-во новых записей: "; cin >> n;
  19.     fstream file("student.bin", ios::binary | ios::out | ios::app);
  20.     cin.clear();
  21.     while (cin.get() != '\n');
  22.     for (int i = 0; i < n; i++)
  23.     {
  24.         while (cin.get() != '\n');
  25.         cin.clear();
  26.         cout << "фио "; cin.getline(student.fullName, N, '\n');
  27.         cout << "курс: "; cin >> student.course;
  28.         cout << "группа "; cin >> student.group;
  29.         file.write(reinterpret_cast<char*>(&student), sizeof(student));
  30.  
  31.     }
  32.     file.close();
  33. }
  34. //*/
  35.  
  36. int numberOfRecords()
  37. {
  38.     Student item;
  39.     ifstream file;
  40.     file.open("student.bin");
  41.     file.seekg(0, ios::end);
  42.     int result = file.tellg() / sizeof(item);
  43.     file.close();
  44.     return result;
  45. }
  46.  
  47. vector<Student> getAllRecordsFromFile()
  48. {
  49.     int countOfRecords = numberOfRecords();
  50.     vector<Student> arrayOfRecords(countOfRecords);
  51.     fstream file("student.bin", ios::binary | ios::in);
  52.     for (int i = 0; i < countOfRecords; i++)
  53.         file.read(reinterpret_cast<char*>(&arrayOfRecords[i]), sizeof(arrayOfRecords[i]));
  54.     file.close();
  55.     return arrayOfRecords;
  56. }
  57.  
  58. int main()
  59. {
  60.     vector<Student> students = getAllRecordsFromFile();
  61.     setlocale(0, "");
  62.     sort(students.begin(), students.end());
  63.     for (int i = 0; i < students.size(); i++)
  64.     {
  65.         cout << "фио " << students[i].fullName;
  66.         cout << "\nкурс: " << students[i].course;
  67.         cout << "\nгруппа " << students[i].group;
  68.         cout << endl;
  69.     }
  70.     fstream file("student1.bin", ios::binary | ios::out);
  71.     file.close();
  72.     file.open("student1.bin", ios::binary | ios::out);
  73.     for (int i = 0; i < students.size(); i++)
  74.         file.write(reinterpret_cast<char*>(&students[i]), sizeof(students[i]));
  75.     file.close();
  76.     return 0;
  77. }
Add Comment
Please, Sign In to add comment