aboutsummaryrefslogtreecommitdiff
path: root/week09/Exercise2/Date.cpp
blob: 22cb5de1d0afad6617f940f79f7b13e280f2bdda (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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#include "Date.h"
#include <iostream>
#include <fstream>

Date::Date() {
	day = month = year = 0;
}

Date::Date(unsigned day, unsigned month, unsigned year) {
	this->day = day;
	this->month = month;
	this->year = year;
}

void Date::print() {
	std::cout << day << " " << month << " " << year << std::endl;
}

void Date::StoreText(const char* outFileName) {
	std::ofstream outFile(outFileName, std::ios::app);
	if (!outFile.is_open()) {
		throw "Couldn't open file!";
	}

	outFile << year << " " << month << " " << day << std::endl;

	outFile.close();
}

void Date::StoreBinary(const char* outFileName) {
	std::ofstream outFile(outFileName, std::ios::binary | std::ios::app);
	if (!outFile.is_open()) {
		throw "Couldn't open file!";
	}

	outFile.write((const char*)&year, sizeof(year));
	outFile.write((const char*)&month, sizeof(month));
	outFile.write((const char*)&day, sizeof(day));

	outFile.close();
}

void Date::LoadText(const char* inFileName) {
	std::fstream file(inFileName, std::ios::in | std::ios::out);
	if (!file.is_open()) {
		throw "Couldn't open file!";
	}

	file >> year >> month >> day;

	while (!file.fail()) {
		file.put(' ');
		file.seekg(-2, std::ios::cur);
	}
	file.clear();

	file.close();
}

void Date::LoadBinary(const char* inFileName) {
	std::ifstream inFile(inFileName);
	if (!inFile.is_open()) {
		throw "Couldn't open file!";
	}

	inFile.read((char*)&year, sizeof(year));
	inFile.read((char*)&month, sizeof(month));
	inFile.read((char*)&day, sizeof(day));

	inFile.close();
}