Boost serialization

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;


Literature.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#include <boost/archive/text_oarchive.hpp>
#include <boost/archive/text_iarchive.hpp>

#include <iostream>
#include <fstream>
#include <string>

class Literature
{
    private:
        friend class boost::serialization::access;

    public:
        Literature() { }
        Literature(std::string t): type(t) { }
        virtual ~Literature() { }

        virtual std::string GetType() const { return type; }

    private:
        template<class Archive>
        void serialize(Archive &ar, const unsigned int version)
        {
            ar & type;
        }

        std::string type;
};


class Book : public Literature
{
    private:
        friend class boost::serialization::access;

    public:
        Book() { }
        Book(std::string t, std::string n, int p): Literature(t), name(n), pages(p) { }
        virtual ~Book() { }

        std::string GetName() const { return name; }
        int GetPages() const { return pages; }

        friend std::ostream &operator<<(std::ostream &, const Book &);

    private:
        template<class Archive>
        void serialize(Archive &ar, const unsigned int version)
        {
            ar & boost::serialization::base_object<Literature>(*this);
            ar & name;
            ar & pages;
        }

        std::string name;
        int pages;
};


main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#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;
}
Bump.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
int main()
{
    {
        std::ofstream ofs("file_test");
        boost::archive::text_oarchive oa(ofs);

        const Book b("SF", "Pandora's Star", 1100);
        const Book b2("SF", "Judas Unchained", 1100);

        oa << b << b2 ;
    }

    std::ifstream ifs("file_test");
    boost::archive::text_iarchive ia(ifs);

    while(ifs)
    {
        try
        {
            Book b ;
            ia >> b ;
            std::cout << b.GetType() << ": " << b.GetName() << '\n' ;
        }
        catch( const std::exception& e )
        {
            std::clog << "*** " << e.what() << '\n' ;
        }
    }
}
Yes it's exactly something like that I'm looking for, but not getting the the correct output.

The output I'm getting is:

Pandora's Star
0 0 0 0 2



Works with the first one, but not the second.


Edit. Hmm just tried to copy your code and it's working. Gonna see if I can find my mistake. :)

Edit2: Got it working. Thank you so much.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
void Write(std::vector<Book> &books, const std::string file_name)
{
    // create and open an archive for output
    std::ofstream ofs(file_name);
    boost::archive::text_oarchive oa(ofs);

    for(auto &elem : books)
    {
        oa << elem;
    }
}

void Read(std::ifstream &ifs)
{
    // restore the class instance to its original state
    boost::archive::text_iarchive ia(ifs);

    while(ifs)
    {
        try
        {
            Book b;
            // read class state from archive
            ia >> b;
            std::cout << b.GetType() << ": " << b.GetName() << "\n";
        }
        catch(const std::exception &e)
        {
            std::clog << e.what() << "\n";
        }
    }
}

int main()
{
    std::string file_name("file_test");

    const Book b("SF", "Pandora's Star", 1100);
    const Book b2("SF", "Judas Unchained", 1100);
    const Book b3("SF", "Void", 1100);
    std::vector<Book> books;
    books.push_back(b);
    books.push_back(b2);
    books.push_back(b3);

    Write(books, file_name);

    // create and open an archive for input
    std::ifstream ifs(file_name);

    Read(ifs);
}
Last edited on
Topic archived. No new replies allowed.