1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
|
#include "Book.h"
#include <iostream>
#include <fstream>
void Book::SetTitle(const String& newTitle) {
title = newTitle;
}
const String& Book::GetTitle() const {
return title;
}
void Book::SetAuthor(const String& newAuthor) {
author = newAuthor;
}
const String& Book::GetAuthor() const {
return author;
}
void Book::SetYear(short newYear) {
year = newYear;
}
short Book::GetYear() const {
return year;
}
void Book::SetText(const String& newText) {
text = newText;
}
const String& Book::GetText() const {
return text;
}
void Book::SetPrice(double newPrice) {
price = newPrice;
}
double Book::GetPrice() const {
return price;
}
void Book::SetAvailableForPurchase(bool isAvailableForPurchase) {
availableForPurchase = isAvailableForPurchase;
}
bool Book::GetAvailableForPurchase() const {
return availableForPurchase;
}
void Book::PrintBookInfo() const {
std::cout << GetTitle().GetStr() << ", "
<< GetAuthor().GetStr() << ' '
<< GetYear() << std::endl;
}
bool Book::Export(const String& fileName) {
std::ofstream exportFile(fileName.GetStr(), std::ios::binary);
if (!exportFile.is_open()) {
std::cout << "Couldn't export book to file!" << std::endl;
}
else {
exportFile.write(title.GetStr(), title.GetLength() + 1);
exportFile.write(author.GetStr(), author.GetLength() + 1);
exportFile.write((const char*)&year, sizeof(year));
exportFile.write(text.GetStr(), text.GetLength() + 1);
}
return exportFile.is_open();
}
void readNullTerminated(String& readInto, std::ifstream& file) {
readInto.Clear();
for (char buffer = file.get(); buffer != '\0' && !file.eof(); buffer = file.get()) {
readInto.AppendChar(buffer);
}
}
bool Book::Import(const String& fileName) {
std::ifstream importFile(fileName.GetStr(), std::ios::binary);
if (!importFile.is_open()) {
std::cout << "Couldn't import book to file!" << std::endl;
}
else {
readNullTerminated(title, importFile);
readNullTerminated(author, importFile);
importFile.read((char*)&year, sizeof(year));
readNullTerminated(text, importFile);
}
return importFile.is_open();
}
|