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/Exercise07/FileString.cpp | 43 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 week10/Exercise07/FileString.cpp (limited to 'week10/Exercise07/FileString.cpp') 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 + +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(); +} -- cgit v1.2.3