Files
cppcourse/397/SolutionEx62/Main.cpp
T
2026-10-05 11:27:21 +02:00

73 lines
1.7 KiB
C++

#include "Person.h"
#include <algorithm>
#include <cctype>
#include <iostream>
#include <list>
// header for file streams
#include <fstream>
// include headers that implement a archive in simple text format
#include <boost/archive/text_iarchive.hpp>
#include <boost/archive/text_oarchive.hpp>
// include header that allows serialization of STL list
#include <boost/serialization/list.hpp>
// STL collection of Person*
std::list<Person *> people;
void add() {
// creates a person, collects the information, adds person to list
Person *newP = new Person;
newP->askForInfo();
people.push_back(newP);
}
void showPerson(Person *p) {
// displays a person
p->display();
}
void display() {
// displays all the people
std::for_each(people.begin(), people.end(), &showPerson);
}
int main() {
// deserialize existing file (if there is one)
try {
// create and open an archive for input
std::ifstream ifs("file.dat");
boost::archive::text_iarchive ia(ifs);
// read class state from archive
ia >> people;
// archive and stream closed when destructors are called
} catch (...) {
}
char choice = 0;
while (choice != 'X') {
switch (choice) {
case 'A':
add();
break;
case 'D':
display();
break;
}
std::cout << "Enter choice: [A]dd, [D]isplay, e[X]it: ";
std::cin >> choice;
choice = std::toupper(choice);
}
// save data to archive
std::ofstream ofs("file.dat");
boost::archive::text_oarchive oa(ofs);
oa << people;
// archive and stream closed when destructors are called
return 0;
}