aboutsummaryrefslogtreecommitdiff
path: root/week10/Exercise10/SaveableString.cpp
diff options
context:
space:
mode:
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();
+}