aboutsummaryrefslogtreecommitdiff
path: root/week10/Exercise07/FileString.cpp
blob: e3d601e6794f041e3b6de506de6da6cd6b03e879 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
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();
}