From e6f1ba664667047c74436f4f35536293f02d846f Mon Sep 17 00:00:00 2001 From: Syndamia Date: Wed, 8 May 2024 09:35:42 +0300 Subject: [w9] Fixed and finished ex3 --- week09/Exercise3/Train.cpp | 26 +++++++++++++++++++++++--- 1 file changed, 23 insertions(+), 3 deletions(-) (limited to 'week09/Exercise3/Train.cpp') diff --git a/week09/Exercise3/Train.cpp b/week09/Exercise3/Train.cpp index 68d4206..4dd793e 100644 --- a/week09/Exercise3/Train.cpp +++ b/week09/Exercise3/Train.cpp @@ -49,18 +49,37 @@ Train::Train(Train&& other) { other.currentRegion = nullptr; } +Train& Train::operator=(Train&& other) { + if (this != &other) { + free(); + + strcpy(this->model, other.model); + this->railID = other.railID; + + this->regionsFileName = other.regionsFileName; + other.regionsFileName = nullptr; + this->currentRegion = other.currentRegion; + other.currentRegion = nullptr; + } + return *this; +} + void Train::setCurrentRegion(std::ifstream& inFile) { int start = inFile.tellg(); while (!inFile.eof() && inFile.peek() != '\n') { inFile.get(); } - inFile.clear(); + if (inFile.eof()) { + inFile.clear(); + inFile.seekg(0, std::ios::end); + } int length = inFile.tellg(); this->currentRegion = new char[length + 1]; inFile.seekg(start, std::ios::beg); - inFile.getline(this->currentRegion, length + 1); + inFile.getline(this->currentRegion, length); + this->currentRegion[length] = '\0'; } Train::Train(const char model[128], unsigned railID, const char* regionsFileName) { @@ -108,7 +127,8 @@ void Train::TransferNextRegion() { throw "Couldn't open regions file!"; } - while (!fileStringEq(inFile, currentRegion)); + while (!fileStringEq(inFile, currentRegion)) { + } setCurrentRegion(inFile); inFile.close(); -- cgit v1.2.3