Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <string>
- #include <iostream>
- #include <fstream>
- #include <list>
- using namespace std;
- class Hospital
- {
- public:
- string doctor_name, patient_name, patient_diagnos, num_palata;
- Hospital(const char* doctor_name = "",
- const char* patient_name = "",
- const char* patient_diagnos = "",
- const char* num_palata = "")
- :doctor_name(doctor_name), patient_name(patient_name), patient_diagnos(patient_diagnos), num_palata(num_palata){}
- void write(ostream& f) const
- {
- writeStr(doctor_name, f);
- writeStr(patient_name, f);
- writeStr(patient_diagnos, f);
- writeStr(num_palata, f);
- }
- void read(istream& f)
- {
- readStr(doctor_name, f);
- readStr(patient_name, f);
- readStr(patient_diagnos, f);
- readStr(num_palata, f);
- }
- static void ReadSortByPatientName(string path) {
- list<Hospital> l;
- ifstream fin;
- fin.open(path);
- if (!fin.is_open())
- cout << "Error open!" << endl;
- else {
- Hospital b;
- while (fin.read((char*)&b, sizeof(Hospital)))
- l.push_back(b);
- SortByPatientName(l);
- for (auto i : l)
- cout << "doctor_name: " << i.doctor_name << "\tpatient_name: " << i.patient_name << "\tpatient_diagnos: " << i.patient_diagnos << "\tnum_palata: " << i.num_palata << endl;
- }
- fin.close();
- }
- friend ostream& operator << (ostream& f, const Hospital& i)
- {
- return f << "doctor_name: " << i.doctor_name << "\tpatient_name: " << i.patient_name << "\tpatient_diagnos: " << i.patient_diagnos << "\tnum_palata: " << i.num_palata;
- }
- private:
- static void writeStr(const string& s, ostream& f)
- {
- size_t l = s.length();
- f.write((const char*)&l, sizeof(size_t));
- f.write(s.data(), l);
- }
- static void readStr(string& s, istream& f)
- {
- size_t l;
- f.read((char*)&l, sizeof(size_t));
- char* str = new char[l + 1];
- f.read(str, l);
- str[l] = 0;
- s = str;
- delete[] str;
- }
- static void SortByPatientName(list<Hospital>& list) {
- list.sort([](Hospital& l1, Hospital& l2) { return l1.patient_name < l2.patient_name; });
- }
- };
- int main(int argc, const char* argv[])
- {
- setlocale(0, "");
- Hospital x("Богданець Віктор Юрійович", "Власик Олексій Юрійович", "Загостренний апендицит", "3"), y("Богданець Віктор Юрійович", "Мельник Олексій Юрійович", "Застуда", "5");
- Hospital u, v, c;
- cout << u << "\n" << v << "\n\n";
- {
- ofstream out("text.txt", ios::binary);
- x.write(out);
- y.write(out);
- }
- {
- ifstream in("text.txt", ios::binary);
- u.read(in);
- v.read(in);
- cout << u << "\n" << v << "\n\n";
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment