Vla_DOS

Untitled

May 10th, 2022
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.62 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <string>
  4. #include <list>
  5. using namespace std;
  6.  
  7. class Hospital {
  8. public:
  9.     string doctor_name;
  10.     string patient_name;
  11.     string patient_diagnos;
  12.     int num_palata;
  13.  
  14.     Hospital() {
  15.         doctor_name = patient_name = patient_diagnos = num_palata = 0;
  16.     }
  17.  
  18.     Hospital(string doctor_name, string patient_name, string patient_diagnos, int num_palata) {
  19.         this->doctor_name = doctor_name;
  20.         this->patient_name = patient_name;
  21.         this->patient_diagnos = patient_diagnos;
  22.         this->num_palata = num_palata;
  23.     }
  24.  
  25.     void Print() {
  26.         cout << "doctor_name: " << doctor_name << "\tpatient_name: " << patient_name << "\tpatient_diagnos: " << patient_diagnos << "\tnum_palata: " << num_palata << endl;
  27.     }
  28.     friend ostream& operator << (ostream& f, const Hospital& i)
  29.     {
  30.         return f << "doctor_name: " << i.doctor_name << "\tpatient_name: " << i.patient_name << "\tpatient_diagnos: " << i.patient_diagnos << "\tnum_palata: " << i.num_palata;
  31.     }
  32. private:
  33.     static void readStr(string& s, istream& f)
  34.     {
  35.         size_t l;
  36.         f.read((char*)&l, sizeof(size_t));
  37.         char* str = new char[l + 1];
  38.         f.read(str, l);
  39.         str[l] = 0;
  40.         s = str;
  41.         delete[] str;
  42.     }
  43. };
  44.  
  45. static void SortByPatientName(list<Hospital>& list) {
  46.     list.sort([](Hospital& l1, Hospital& l2) { return l1.patient_name < l2.patient_name; });
  47. }
  48. void Write(string path) {
  49.     string doctor_name, patient_name, patient_diagnos, num_palata;
  50.     //cin >> doctor_name;
  51.     //cin >> patient_name;
  52.     //cin >> patient_diagnos;
  53.     //cin >> num_palata;
  54.  
  55.     Hospital bf("d", "4", "4", 4);
  56.     //bf.Print();
  57.  
  58.     ofstream fout;
  59.     fout.open(path, ofstream::app);
  60.  
  61.     if (!fout.is_open())
  62.     {
  63.         cout << "Error open!" << endl;
  64.     }
  65.     else {
  66.         fout.write((char*)&bf, sizeof(Hospital));
  67.     }
  68.     fout.close();
  69. }
  70. void Read(string path) {
  71.     list<Hospital> l;
  72.  
  73.     ifstream fin;
  74.     fin.open(path);
  75.  
  76.     if (!fin.is_open())
  77.     {
  78.         cout << "Error open!" << endl;
  79.     }
  80.     else {
  81.         Hospital b;
  82.  
  83.         while (fin.read((char*)&b, sizeof(Hospital)))
  84.         {
  85.             l.push_back(b);
  86.         }
  87.         for (auto i : l)
  88.             i.Print();
  89.         //SortByPatientName(l);
  90.         //cout << endl;
  91.         //for (auto i : l)
  92.         //    i.Print();
  93.     }
  94.     fin.close();
  95. }
  96. int main()
  97. {
  98.     string path = "text.txt";
  99.     Write(path);
  100.     cout << "12";
  101.  
  102.     Read(path);
  103.  
  104.     int log;
  105.     cout << "12";
  106.  
  107.     cin >> log;
  108.  
  109.     cout << "12";
  110.  
  111.     return 0;
  112. }
Advertisement
Add Comment
Please, Sign In to add comment