diff options
| author | Syndamia <kamen@syndamia.com> | 2024-04-03 17:47:07 +0300 |
|---|---|---|
| committer | Syndamia <kamen@syndamia.com> | 2024-04-03 17:47:07 +0300 |
| commit | 44d085f265583f0e3cbef294bbe2c8e300aaa452 (patch) | |
| tree | 4899165f82a51beca1d4726db441a2749b628b9f /week06/Exercise09.cpp | |
| parent | 6a8154ad7f2cbfbb2ae4f2ddda1cd0db0e430e44 (diff) | |
| download | oop-2023-solutions-44d085f265583f0e3cbef294bbe2c8e300aaa452.tar oop-2023-solutions-44d085f265583f0e3cbef294bbe2c8e300aaa452.tar.gz oop-2023-solutions-44d085f265583f0e3cbef294bbe2c8e300aaa452.zip | |
[w6] Added exercise descriptions and solutions to 1-9
Diffstat (limited to 'week06/Exercise09.cpp')
| -rw-r--r-- | week06/Exercise09.cpp | 55 |
1 files changed, 55 insertions, 0 deletions
diff --git a/week06/Exercise09.cpp b/week06/Exercise09.cpp new file mode 100644 index 0000000..97a9c27 --- /dev/null +++ b/week06/Exercise09.cpp @@ -0,0 +1,55 @@ +#include <cstring> +#include <fstream> +#include <iostream> + +struct Person { +private: + char name[1024]; + unsigned age; + +public: + Person(const char name[1024], unsigned age) { + strcpy(this->name, name); + this->age = age; + } + + const char* GetName() { + return name; + } + unsigned GetAge() { + return age; + } + + void Save(const char* fileName) { + std::ofstream outFile(fileName, std::ios::binary); + if (!outFile.is_open()) { + std::cout << "Couldn't open file!" << std::endl; + return; + } + + outFile.write((const char*)&age, sizeof(age)); + outFile.write((const char*)&name, sizeof(name)); + outFile.close(); + } + + void Load(const char* fileName) { + std::ifstream inFile(fileName, std::ios::binary); + if (!inFile.is_open()) { + std::cout << "Couldn't open file!" << std::endl; + return; + } + + inFile.read((char*)&age, sizeof(age)); + inFile.read((char*)&name, sizeof(name)); + } +}; + +int main() { + Person p1("Jordan", 22); + p1.Save("ex9.dat"); + + Person p2("Michael", 89); + p2.Load("ex9.dat"); + + std::cout << p2.GetName() << " " << p2.GetAge() << std::endl; +} |
