Advertisement
Guest User

Untitled

a guest
May 11th, 2023
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.49 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <vector>
  4. #include <string>
  5.  
  6. class Contact {
  7. public:
  8.     Contact() {
  9.         this->value = 0;
  10.         this->name = "";
  11.     }
  12.     Contact(int value, std::string name)
  13.     {
  14.         this->value = value;
  15.         this->name = name;
  16.     }
  17.     ~Contact() {};
  18. private:
  19.     int value;
  20.     std::string name;
  21.    
  22. };
  23.  
  24. class ContactBook {
  25. public:
  26.     int id;
  27.     std::string name;
  28.     std::vector<Contact> contacts;
  29.     ContactBook() {
  30.         this->id = 0;
  31.         this->name = "";
  32.         //this->contacts;
  33.     }
  34.     ~ContactBook() {};
  35. };
  36.  
  37. void saveObjects(std::vector<ContactBook>& books, std::string fileName) {
  38.     std::ofstream file(fileName, std::ios::binary | std::ios::trunc);
  39.  
  40.     if (!file.is_open()) {
  41.         std::cerr << "Nie można otworzyć pliku" << std::endl;
  42.         return;
  43.     }
  44.  
  45.     size_t size = books.size();
  46.     file.write(reinterpret_cast<char*>(&size), sizeof(size));
  47.  
  48.     for (const auto& book : books) {
  49.         file.write(reinterpret_cast<const char*>(&book), sizeof(book));
  50.  
  51.         size_t innerSize = book.contacts.size();
  52.         file.write(reinterpret_cast<char*>(&innerSize), sizeof(innerSize));
  53.  
  54.         for (const auto& contact : book.contacts) {
  55.             file.write(reinterpret_cast<const char*>(&contact), sizeof(contact));
  56.         }
  57.     }
  58.  
  59.     file.close();
  60. }
  61.  
  62. std::vector<ContactBook> loadFromFile(std::string fileName)
  63. {
  64.     std::vector<ContactBook> contactBooks;
  65.  
  66.     std::ifstream inputFile(fileName, std::ios::binary);
  67.     if (inputFile.is_open()) {
  68.         size_t numBooks;
  69.         if (!inputFile.read(reinterpret_cast<char*>(&numBooks), sizeof(numBooks))) {
  70.             std::cout << "Błąd odczytu liczby obiektów!" << std::endl;
  71.             return contactBooks;
  72.         }
  73.  
  74.         for (size_t i = 0; i < numBooks; i++) {
  75.             ContactBook book;
  76.  
  77.             if (!inputFile.read(reinterpret_cast<char*>(&book), sizeof(book))) {
  78.                 std::cout << "Błąd odczytu pola 'id' obiektu " << i << std::endl;
  79.                 return contactBooks;
  80.             }
  81.  
  82.             size_t numContacts;
  83.  
  84.             if (!inputFile.read(reinterpret_cast<char*>(&numContacts), sizeof(numContacts))) {
  85.                 std::cout << "Błąd odczytu liczby obiektów wewnętrznych obiektu " << i << std::endl;
  86.                 return contactBooks;
  87.             }
  88.  
  89.             contactBooks.push_back(book);
  90.         }
  91.  
  92.         inputFile.close();
  93.     }
  94.     else {
  95.         std::cout << "Nie można otworzyć pliku " << fileName << std::endl;
  96.     }
  97.  
  98.     return contactBooks;
  99. }
  100.  
  101.  
  102. int main(int argc, char* argv[]) {
  103.     std::ifstream inputFile("data.bin", std::ios::binary);
  104.     if (!inputFile.is_open()) {
  105.         // obsługa błędu, jeśli nie udało się otworzyć pliku
  106.     }
  107.     std::vector<ContactBook> books;
  108.  
  109.     Contact cnt1(1, "kontakt 1");
  110.     Contact cnt2(2, "kontakt 2");
  111.     ContactBook book;
  112.  
  113.     book.id = 12412;
  114.     book.name = "nazwa";
  115.  
  116.     book.contacts.push_back(cnt1);
  117.     book.contacts.push_back(cnt2);
  118.  
  119.     books.push_back(book);
  120.  
  121.     saveObjects(books, "data.bin");
  122.  
  123.     //books.clear();
  124.  
  125.     std::vector<ContactBook> books2;
  126.  
  127.     books2 = loadFromFile("data.bin");
  128.  
  129.     // wypisanie na ekranie
  130.     for (auto& obj : books2) {
  131.         std::cout << "\nksiazka.id = " << obj.id << std::endl;
  132.         for (auto& innerObj : obj.contacts) {
  133.             //std::cout << "    contact.value = " << innerObj.value;
  134.         }
  135.     }
  136.  
  137.     return true;
  138. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement