From 88f15e35713c9632216931d26443dc588238732f Mon Sep 17 00:00:00 2001 From: Syndamia Date: Tue, 7 May 2024 22:17:12 +0300 Subject: [w10] Added rough solutions to ex 1-10 --- week10/Exercise10/SaveableString.cpp | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 week10/Exercise10/SaveableString.cpp (limited to 'week10/Exercise10/SaveableString.cpp') diff --git a/week10/Exercise10/SaveableString.cpp b/week10/Exercise10/SaveableString.cpp new file mode 100644 index 0000000..4fa41e8 --- /dev/null +++ b/week10/Exercise10/SaveableString.cpp @@ -0,0 +1,32 @@ +#include "SaveableString.h" +#include +#include + +void SaveableString::write(const char* fileName) { + std::ofstream outFile(fileName); + if (!outFile.is_open()) { + throw "Coudln't open file!"; + } + + outFile.write(fileName, sizeof(char) * strlen(str)); + + outFile.close(); +} + +void SaveableString::read(const char* fileName) { + std::ifstream inFile(fileName); + if (!inFile.is_open()) { + throw "Coudln't open file!"; + } + + inFile.seekg(0, std::ios::end); + unsigned length = inFile.tellg(); + inFile.seekg(0, std::ios::beg); + + free(); + str = new char[length + 1]; + inFile.read(str, sizeof(char) * length); + str[length] = '\0'; + + inFile.close(); +} -- cgit v1.2.3