Boost logo

Boost Users :

Subject: Re: [Boost-users] [serialization] appending elements
From: Robert Ramey (ramey_at_[hidden])
Date: 2009-03-09 00:47:41

Agustín K-ballo Bergé wrote:
> Is it possible to append serialized elements to an existing archive?

the serialization library doesn't currently address this situation.

> need to keep a set of records on a file, and periodically add new
> entries to it (kind of like a database table). Reading the whole set,
> adding the new records, and serializing the set back is not an option
> since I expect the set to go pretty big.

I understand and sympathise.

> I have tried to keep several archives at the same stream (with
> no_header and no_tracking flags),

I believe this might be made to work.

> but then I don't know how to read
> them. Reading them as if they were a single archive does not work,
> should it?


Here's an idea.

// append a bunch of archives to a file
    ofstream os(..); // open for append
        oarchive oa(os)
         oa << a bunch of stuff
    } // close archive leaving stream open
        oarchive oa(os)
        oa << a bunch of other stuff
    } // close second archive
} // now we have a closed stream with several self-contained archives in it

// read a bunch of archives from one file
    ifstream is(...); // open the normal way
        iarchive ia(..); // open first archive
        ia >> first bunch of stuff
    } // close first archive leaving stream open
        iarchive ia(..); // open first archive
        ia >> second bunch of stuff
    } // close second archive leaving stream open
} // close input stream

exercise for the reader. Figure out where to save the Number of archives
file. A couple of ideas
    1) a separate companion file
    2) save a few bytes at the beging of the file - call this archive count.
Update this
after every "append" and read it duning load.

Good Luck with this.

Robert Ramey

> Agustín K-ballo Bergé.-

Boost-users list run by williamkempf at, kalb at, bjorn.karlsson at, gregod at, wekempf at