aboutsummaryrefslogtreecommitdiff
path: root/week10/Exercise10/SaveableString.cpp
blob: 4fa41e8f47e94c707aa3c5f8c00c1fe6111ad908 (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
#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();
}