blob: 32d39f00586b43e09817e65b265c440d9ee51107 (
plain) (
blame)
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
|
#include "BookList.h"
/* Private */
void BookList::resize() {
this->bookLength = (this->bookLength == 0) ? 8 : this->bookLength << 1;
Book* newBooks = new Book[this->bookLength];
for (unsigned i = 0; i < this->bookCount; i++)
newBooks[i] = this->books[i];
delete[] this->books;
this->books = newBooks;
}
void BookList::readFromFile(std::ifstream& inFile) {
char nameBuffer[100], authorBuffer[100];
unsigned countBuffer;
while(!inFile.eof()) {
inFile.read(nameBuffer, sizeof(nameBuffer));
inFile.read(authorBuffer, sizeof(authorBuffer));
inFile.read((char*)&countBuffer, sizeof(countBuffer));
this->addBook(Book(nameBuffer, authorBuffer, countBuffer));
}
}
void BookList::writeToFile(std::ofstream& outFile) const {
unsigned countBuffer;
for (int i = 0; i < this->bookCount; i++) {
outFile.write(this->books[i].get_name(), sizeof(this->books[i].get_name()));
outFile.write(this->books[i].get_author(), sizeof(this->books[i].get_author()));
countBuffer = this->books[i].get_count();
outFile.write((const char*)&countBuffer, sizeof(countBuffer));
}
}
void BookList::writePopularToFile(std::ofstream& outFile) const {
for (int i = 0; i < this->bookCount; i++) {
if (this->books[i].get_count() >= 1000) {
outFile << this->books[i].get_name() << " " << this->books[i].get_author() << " " << this->books[i].get_count() << std::endl;
}
}
}
BookList::BookList()
{}
// Не ми стигна времето, но другите функции са аналогични на тeзи двете
bool BookList::readFromFile(const char* filePath) {
std::ifstream file(filePath, std::ios::binary);
if (!file.is_open())
return false;
this->readFromFile(file);
file.close();
return true;
}
bool BookList::writePopularToFile(const char* filePath) const {
std::ofstream file(filePath);
if (!file.is_open())
return false;
this->writePopularToFile(file);
file.close();
return true;
}
|