From e0964bbc4f66e7d193abd249213c9cf762fd0294 Mon Sep 17 00:00:00 2001 From: crupest Date: Wed, 30 Dec 2020 15:37:39 +0800 Subject: import(life): ... --- works/life/cpp-practicum/Record.cpp | 38 +++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 works/life/cpp-practicum/Record.cpp (limited to 'works/life/cpp-practicum/Record.cpp') diff --git a/works/life/cpp-practicum/Record.cpp b/works/life/cpp-practicum/Record.cpp new file mode 100644 index 0000000..1041b23 --- /dev/null +++ b/works/life/cpp-practicum/Record.cpp @@ -0,0 +1,38 @@ +#include "Record.hpp" + +void Record::WriteTo(QFile file) { + file.open(QFile::ReadWrite | QFile::Text | QFile::Truncate); + QTextStream stream(&file); + stream.setCodec("UTF-8"); + + stream << books_.size() << ' ' << vendors_.size() << '\n'; + for (const auto &book : books_) { + stream << book << '\n'; + } + for (const auto &vendor : vendors_) { + stream << vendor << '\n'; + } +} + +void Record::ReadFrom(QFile file) { + file.open(QFile::ReadOnly | QFile::Text); + QTextStream stream(&file); + stream.setCodec("UTF-8"); + + books_.clear(); + vendors_.clear(); + + int book_count, vendor_count; + stream >> book_count >> vendor_count; + stream.skipWhiteSpace(); + for (int i = 0; i < book_count; i++) { + Book book; + stream >> book; + books_.push_back(std::move(book)); + } + for (int i = 0; i < vendor_count; i++) { + Vendor vendor; + stream >> vendor; + vendors_.push_back(std::move(vendor)); + } +} -- cgit v1.2.3