aboutsummaryrefslogtreecommitdiff
path: root/week10/Exercise07/FileString.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/Exercise07/FileString.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/Exercise07/FileString.cpp')
-rw-r--r--week10/Exercise07/FileString.cpp43
1 files changed, 43 insertions, 0 deletions
diff --git a/week10/Exercise07/FileString.cpp b/week10/Exercise07/FileString.cpp
new file mode 100644
index 0000000..e3d601e
--- /dev/null
+++ b/week10/Exercise07/FileString.cpp
@@ -0,0 +1,43 @@
+#include "FileString.h"
+#include <fstream>
+
+FileString::FileString(const char* fileName) : String() {
+ this->fileName = fileName; // Конвертиращ конструктор!
+
+ std::ifstream inFile(fileName);
+ if (!inFile.is_open()) {
+ throw "Couldn't open file!";
+ }
+
+ while (!inFile.eof() && inFile.peek() != '\n') {
+ inFile.get();
+ }
+
+ if (inFile.eof()) {
+ inFile.seekg(0, std::ios::end);
+ this->length = inFile.peek();
+ }
+ else {
+ this->length = inFile.peek() - 1;
+ }
+
+ inFile.seekg(0, std::ios::beg);
+ str = new char[this->length + 1];
+ inFile.getline(str, this->length);
+ str[this->length] = '\0';
+
+ inFile.close();
+}
+
+void FileString::ChangeAt(unsigned index, char newValue) {
+ std::fstream file(fileName.GetPtr());
+ if (!file.is_open()) {
+ throw "Couldn't open file!";
+ }
+
+ file.seekp(index, std::ios::beg);
+ file.put(newValue);
+ str[index] = newValue;
+
+ file.close();
+}