#include "Person.h" #include #include #include #include // header for file streams #include // include headers that implement a archive in simple text format #include #include // include header that allows serialization of STL list #include // STL collection of Person* std::list 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; }