Toliak

__7

Dec 19th, 2018
275
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.96 KB | None | 0 0
  1. #include <fstream>
  2. #include <iostream>
  3. #include <string>
  4.  
  5. using namespace std;
  6.  
  7. struct SpecSchool
  8. {
  9.     string city;
  10.     int number;
  11.     string spec;
  12.     int students;
  13. };
  14.  
  15. void write()
  16. {
  17.     int amount;
  18.     cout << "Amount of schools: ";
  19.     cin >> amount;
  20.  
  21.     SpecSchool *array = new SpecSchool[amount];
  22.     for (int i = 0; i < amount; i++) {
  23.         cout << "Element " << i << endl;
  24.         cout << "City: ";
  25.         cin.ignore();
  26.         getline(cin, array[i].city);
  27.         cout << "Number: ";
  28.         cin >> array[i].number;
  29.         cout << "Spec: ";
  30.         cin.ignore();
  31.         getline(cin, array[i].spec);
  32.         cout << "Students: ";
  33.         cin >> array[i].students;
  34.     }
  35.  
  36.     ofstream text("lab7.txt");
  37.     text << amount << endl;
  38.     for (int i = 0; i < amount; i++) {
  39.         text << array[i].city << endl;
  40.         text << array[i].number << endl;
  41.         text << array[i].spec << endl;
  42.         text << array[i].students << endl;
  43.     }
  44.  
  45.     ofstream bin("lab7.bin", ios::binary);
  46.     bin.write((char *) &amount, sizeof(int));
  47.     for (int i = 0; i < amount; i++) {
  48.         bin << array[i].city << endl;
  49.         bin.write((char *) &array[i].number, sizeof(int));
  50.         bin << array[i].spec << endl;
  51.         bin.write((char *) &array[i].students, sizeof(int));
  52.     }
  53.  
  54.     delete[] array;
  55. }
  56.  
  57. void read()
  58. {
  59.     int amount;
  60.  
  61.     cout << "Text file: " << endl;
  62.     ifstream text("lab7.txt");
  63.     text >> amount;
  64.  
  65.     SpecSchool *array = new SpecSchool[amount];
  66.     for (int i = 0; i < amount; i++) {
  67.         text.ignore();
  68.         getline(text, array[i].city);
  69.         text >> array[i].number;
  70.         text.ignore();
  71.         getline(text, array[i].spec);
  72.         text >> array[i].students;
  73.     }
  74.  
  75.     for (int i = 0; i < amount; i++) {
  76.         cout << "Element " << i << endl;
  77.         cout << "City: " << array[i].city << endl;
  78.         cout << "Number: " << array[i].number << endl;
  79.         cout << "Spec: " << array[i].spec << endl;
  80.         cout << "Students: " << array[i].students << endl;
  81.     }
  82.  
  83.  
  84.     delete[] array;
  85.  
  86.     cout << endl << endl << "Bin file:" << endl;
  87.  
  88.     ifstream bin("lab7.bin", ios::binary);
  89.     bin.read((char *) &amount, sizeof(int));
  90.     for (int i = 0; i < amount; i++) {
  91.         getline(bin, array[i].city);
  92.         bin.read((char *) &array[i].number, sizeof(int));
  93.         getline(bin, array[i].spec);
  94.         bin.read((char *) &array[i].students, sizeof(int));
  95.     }
  96.  
  97.     for (int i = 0; i < amount; i++) {
  98.         cout << "Element " << i << endl;
  99.         cout << "City: " << array[i].city << endl;
  100.         cout << "Number: " << array[i].number << endl;
  101.         cout << "Spec: " << array[i].spec << endl;
  102.         cout << "Students: " << array[i].students << endl;
  103.     }
  104.  
  105.     delete[] array;
  106. }
  107.  
  108. int main()
  109. {
  110.     cout << "Read(0) or write(1): ";
  111.     int state;
  112.     cin >> state;
  113.     if (state == 0) {
  114.         read();
  115.     } else {
  116.         write();
  117.     }
  118.  
  119.     return 0;
  120. }
Add Comment
Please, Sign In to add comment