diff options
Diffstat (limited to 'week09/Exercise2')
| -rw-r--r-- | week09/Exercise2/Date.cpp | 71 | ||||
| -rw-r--r-- | week09/Exercise2/Date.h | 18 | ||||
| -rw-r--r-- | week09/Exercise2/main.cpp | 29 |
3 files changed, 118 insertions, 0 deletions
diff --git a/week09/Exercise2/Date.cpp b/week09/Exercise2/Date.cpp new file mode 100644 index 0000000..22cb5de --- /dev/null +++ b/week09/Exercise2/Date.cpp @@ -0,0 +1,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(); +} diff --git a/week09/Exercise2/Date.h b/week09/Exercise2/Date.h new file mode 100644 index 0000000..d280c71 --- /dev/null +++ b/week09/Exercise2/Date.h @@ -0,0 +1,18 @@ +#pragma once + +class Date { + unsigned day; + unsigned month; + unsigned year; + +public: + Date(); + Date(unsigned day, unsigned month, unsigned year); + + void print(); + + void StoreText(const char* outFileName); + void StoreBinary(const char* outFileName); + void LoadText(const char* inFileName); + void LoadBinary(const char* inFileName); +}; diff --git a/week09/Exercise2/main.cpp b/week09/Exercise2/main.cpp new file mode 100644 index 0000000..981e252 --- /dev/null +++ b/week09/Exercise2/main.cpp @@ -0,0 +1,29 @@ +#include "Date.h" + +int main() { + Date d1(5, 12, 2012); + Date d2(18, 3, 1999); + Date d3(10, 1, 2020); + Date d4(30, 7, 1975); + Date d5(2, 8, 1300); + + d1.StoreText("dates.txt"); + d2.StoreText("dates.txt"); + d3.StoreText("dates.txt"); + d4.StoreText("dates.txt"); + d5.StoreText("dates.txt"); + + Date nd1, nd2, nd3, nd4, nd5; + + nd1.LoadText("dates.txt"); + nd2.LoadText("dates.txt"); + nd3.LoadText("dates.txt"); + nd4.LoadText("dates.txt"); + nd5.LoadText("dates.txt"); + + nd5.print(); + nd4.print(); + nd3.print(); + nd2.print(); + nd1.print(); +} |
