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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
|
#include <iostream>
#include <fstream>
#include <cstring>
// Exercise 1
int fileSize(std::ifstream& file) {
int currPos = file.tellg();
file.seekg(0, std::ios::end);
int size = file.tellg();
file.seekg(currPos, std::ios::beg);
return size;
}
void duplicate(const char* filePath, const char* newFile, int n) {
std::ifstream inFile(filePath);
int inFileSize = fileSize(inFile);
std::ofstream outFile(newFile);
for (int i = 0; i < inFileSize * n; i++) {
outFile.put(inFile.get());
if (inFile.tellg() == inFileSize)
inFile.seekg(0, std::ios::beg);
}
outFile.close();
inFile.close();
}
void ex1() {
duplicate("dupeSrc.txt", "dupeDest.txt", 3);
}
// Exercise 2
struct Task {
private:
char* name = nullptr;
size_t nameLen;
char* description = nullptr;
size_t descriptionLen;
int points;
void setStringProp(char*& prop, size_t& propLen, const char* newVal, int valSize) {
delete[] prop;
prop = new char[valSize];
for (int i = 0; i < valSize; i++)
prop[i] = newVal[i];
propLen = valSize;
}
public:
Task() {
this->set_name("", 1);
this->set_description("", 1);
this->set_points(0);
}
Task(const char* name, const char* desc, int points) {
this->set_name(name, strlen(name) + 1); // +1 so we also capture the '\0'
this->set_description(desc, strlen(desc) + 1); // +1 so we also capture the '\0'
this->set_points(points);
}
~Task() {
delete[] this->name;
delete[] this->description;
}
void writeToFile(std::ofstream& file) const {
file.write((const char*)&this->nameLen, sizeof(this->nameLen));
file.write(this->name, nameLen);
file.write((const char*)&this->descriptionLen, sizeof(this->descriptionLen));
file.write(this->description, descriptionLen);
file.write((const char*)&this->points, sizeof(this->points));
}
void writeToFile(const char* name) const {
std::ofstream file(name);
this->writeToFile(file);
file.close();
}
void readFromFile(std::ifstream& file) {
file.read((char*)&this->nameLen, sizeof(this->nameLen));
file.read(this->name, this->nameLen);
file.read((char*)&this->descriptionLen, sizeof(this->descriptionLen));
file.read(this->description, this->descriptionLen);
file.read((char*)&this->points, sizeof(this->points));
}
void readFromFile(const char* fileName) {
std::ifstream file(fileName);
this->readFromFile(file);
file.close();
}
void set_name(const char* newName, int newNameSize) {
this->setStringProp(this->name, this->nameLen, newName, newNameSize);
}
const char* get_name() const {
return this->name;
}
int get_nameLen() const {
return this->nameLen;
}
void set_description(const char* newDesc, int newDescSize) {
this->setStringProp(this->description, this->descriptionLen, newDesc, newDescSize);
}
const char* get_description() const {
return this->description;
}
int get_descriptionLen() const {
return this->descriptionLen;
}
void set_points(int points) {
this->points = points;
}
int get_points() const {
return this->points;
}
};
const int EXAM_TASK_CNT = 3;
struct Exam {
private:
Task tasks[EXAM_TASK_CNT];
int passingPoints;
public:
Exam() {}
Exam(const Task* elems, int passingPoints) {
for (int i = 0; i < EXAM_TASK_CNT; i++) {
this->tasks[i].set_name(elems[i].get_name(), elems[i].get_nameLen());
this->tasks[i].set_description(elems[i].get_description(), elems[i].get_descriptionLen());
this->tasks[i].set_points(elems[i].get_points());
}
this->set_passingPoints(passingPoints);
}
void writeToFile(std::ofstream& file) const {
file.write((const char*)&this->passingPoints, sizeof(this->passingPoints));
for (int i = 0; i < EXAM_TASK_CNT; i++)
this->tasks[i].writeToFile(file);
}
void writeToFile(const char* fileName) const {
std::ofstream file(fileName, std::ios::binary);
this->writeToFile(file);
file.close();
}
void readFromFile(std::ifstream& file) {
file.read((char*)&this->passingPoints, sizeof(this->passingPoints));
for (int i = 0; i < EXAM_TASK_CNT; i++)
this->tasks[i].readFromFile(file);
}
void readFromFile(const char* fileName) {
std::ifstream file(fileName, std::ios::binary);
this->readFromFile(file);
file.close();
}
void set_passingPoints(int points) {
this->passingPoints = points;
}
int get_passingPoints() const {
return this->passingPoints;
}
int getMax() {
int acc = 0;
for (int i = 0; i < EXAM_TASK_CNT; i++)
acc += this->tasks[i].get_points();
return acc;
}
};
void ex2() {
Task tasks[EXAM_TASK_CNT] = { Task("Wow1", "Bruh1", 1), Task("Wow2", "Bruh2", 2), Task("Wow3", "Bruh3", 3) };
Exam ex(tasks, 4);
ex.writeToFile("test.txt");
Exam ex1;
ex1.readFromFile("test.txt");
ex1.writeToFile("test1.txt");
std::cout << ex1.getMax() << std::endl;
}
int main() {
ex2();
}
|