aboutsummaryrefslogtreecommitdiff
path: root/week10/Exercise10/SaveableString.cpp
diff options
context:
space:
mode:
authorSyndamia <kamen@syndamia.com>2024-05-07 22:17:12 +0300
committerSyndamia <kamen@syndamia.com>2024-05-07 22:17:12 +0300
commit88f15e35713c9632216931d26443dc588238732f (patch)
tree147ff8c72009afc58d3eabb1c026a33b798f20fe /week10/Exercise10/SaveableString.cpp
parentf4642325f58172e85b9e41e35f69e8bea46f78c6 (diff)
downloadoop-2023-solutions-88f15e35713c9632216931d26443dc588238732f.tar
oop-2023-solutions-88f15e35713c9632216931d26443dc588238732f.tar.gz
oop-2023-solutions-88f15e35713c9632216931d26443dc588238732f.zip
[w10] Added rough solutions to ex 1-10
Diffstat (limited to 'week10/Exercise10/SaveableString.cpp')
-rw-r--r--week10/Exercise10/SaveableString.cpp32
1 files changed, 32 insertions, 0 deletions
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 <cstring>
+#include <fstream>
+
+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();
+}