Need some help with my program.
Im trying to create a ReadFile function that will read all the information in the archive and print it to the screen. But so far i can only get it to write the first object.
Is it possible to get a pointer to the archive so you can iterate over it, or some other way so I can get access to all the saved objects in the archive?
Maybe something like: while( ia >> newb )
1 2
// This is where i want to iterate over the archive in the ReadFile function.
ia >> newb;
#include <iostream>
#include <fstream>
#include <string>
#include <list>
#include <iterator>
#include "Literature.hpp"
void WriteFile(const Book &b, std::ofstream &ofs)
{
// save date to archive
boost::archive::text_oarchive oa(ofs);
// write class instance to archive
oa << b;
//archive and stream closed when destructors are called
}
void ReadFile(std::ifstream &ifs)
{
// restore the class instance to its original state
Book newb;
boost::archive::text_iarchive ia(ifs);
// read class state from archive
ia >> newb;
std::cout << newb;
}
int main()
{
// create and open an archive for output
std::ofstream ofs("file_test");
// create class instance
const Book b("SF", "Pandora's Star", 1100);
const Book b2("SF", "Judas Unchained", 1100);
WriteFile(b, ofs);
WriteFile(b2, ofs);
// create and open an archive for input
std::ifstream ifs("file_test");
ReadFile(ifs);
return 0;
}