#include #include #include // 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(); }