Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <fstream>
- #include <vector>
- #include <string>
- class Contact {
- public:
- Contact() {
- this->value = 0;
- this->name = "";
- }
- Contact(int value, std::string name)
- {
- this->value = value;
- this->name = name;
- }
- ~Contact() {};
- private:
- int value;
- std::string name;
- };
- class ContactBook {
- public:
- int id;
- std::string name;
- std::vector<Contact> contacts;
- ContactBook() {
- this->id = 0;
- this->name = "";
- //this->contacts;
- }
- ~ContactBook() {};
- };
- void saveObjects(std::vector<ContactBook>& books, std::string fileName) {
- std::ofstream file(fileName, std::ios::binary | std::ios::trunc);
- if (!file.is_open()) {
- std::cerr << "Nie można otworzyć pliku" << std::endl;
- return;
- }
- size_t size = books.size();
- file.write(reinterpret_cast<char*>(&size), sizeof(size));
- for (const auto& book : books) {
- file.write(reinterpret_cast<const char*>(&book), sizeof(book));
- size_t innerSize = book.contacts.size();
- file.write(reinterpret_cast<char*>(&innerSize), sizeof(innerSize));
- for (const auto& contact : book.contacts) {
- file.write(reinterpret_cast<const char*>(&contact), sizeof(contact));
- }
- }
- file.close();
- }
- std::vector<ContactBook> loadFromFile(std::string fileName)
- {
- std::vector<ContactBook> contactBooks;
- std::ifstream inputFile(fileName, std::ios::binary);
- if (inputFile.is_open()) {
- size_t numBooks;
- if (!inputFile.read(reinterpret_cast<char*>(&numBooks), sizeof(numBooks))) {
- std::cout << "Błąd odczytu liczby obiektów!" << std::endl;
- return contactBooks;
- }
- for (size_t i = 0; i < numBooks; i++) {
- ContactBook book;
- if (!inputFile.read(reinterpret_cast<char*>(&book), sizeof(book))) {
- std::cout << "Błąd odczytu pola 'id' obiektu " << i << std::endl;
- return contactBooks;
- }
- size_t numContacts;
- if (!inputFile.read(reinterpret_cast<char*>(&numContacts), sizeof(numContacts))) {
- std::cout << "Błąd odczytu liczby obiektów wewnętrznych obiektu " << i << std::endl;
- return contactBooks;
- }
- contactBooks.push_back(book);
- }
- inputFile.close();
- }
- else {
- std::cout << "Nie można otworzyć pliku " << fileName << std::endl;
- }
- return contactBooks;
- }
- int main(int argc, char* argv[]) {
- std::ifstream inputFile("data.bin", std::ios::binary);
- if (!inputFile.is_open()) {
- // obsługa błędu, jeśli nie udało się otworzyć pliku
- }
- std::vector<ContactBook> books;
- Contact cnt1(1, "kontakt 1");
- Contact cnt2(2, "kontakt 2");
- ContactBook book;
- book.id = 12412;
- book.name = "nazwa";
- book.contacts.push_back(cnt1);
- book.contacts.push_back(cnt2);
- books.push_back(book);
- saveObjects(books, "data.bin");
- //books.clear();
- std::vector<ContactBook> books2;
- books2 = loadFromFile("data.bin");
- // wypisanie na ekranie
- for (auto& obj : books2) {
- std::cout << "\nksiazka.id = " << obj.id << std::endl;
- for (auto& innerObj : obj.contacts) {
- //std::cout << " contact.value = " << innerObj.value;
- }
- }
- return true;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement