Boost logo

Boost Users :

Subject: Re: [Boost-users] Seeking in a serialized file
From: Pfligersdorffer, Christian (Christian.Pfligersdorffer_at_[hidden])
Date: 2012-02-21 06:36:11


gnu indian <gnuindian_at_[hidden]> wrote:
>
> hi,
> I am fairly new to boost and using it for serialization. Though I went through the archives, couldn't get a
> clear answer to the following:
>
> How do I fseek in a file and load from an offset. I don't want to read the whole data in the file; which
> works ( as given in some tutorial); but normal fseekg and then loading apparently doesn't work.
> Here is the sample code:

Hi Mridul,

the wanted behavior must be implemented outside of boost::serialization but is not complicated at all. The trick is to create multiple archives - everything that needs to be deserialized as one chunck needs to be serialized as one chunck. Roberts library is very symmetric in everything it does, as a quick rule. ;-)

So if you modify your code slightly it will work:

>
> const char* fileName = "xyz.txt";
>
> // Create some objects
> const Obj o1(1, false);
> const Obj o2;
> const Obj o3(2, true);
> const Obj* const p1 = &o1;
> int p = 0;
> int q = 0;
>
> // Save data
> {
> // Create an output archive
> std::fstream ofs(fileName);
>

       {
            boost::archive::binary_oarchive ar(ofs,boost::archive::no_header);
           // Write data
           ar & o1 & o2 & o3 & p1;
        }
       p = ofs.tellp();

       {
            boost::archive::binary_oarchive ar(ofs,boost::archive::no_header);
           // Write data
           ar & o1 & o2 & o3 & p1;
        }
> }
>
>
> // Restore data
> Obj restored_o1;
> Obj restored_o2;
> Obj restored_o3;
> Obj* restored_p1;
> {
> // Create and input archive
> std::ifstream ifs(fileName);
> boost::archive::binary_iarchive ar(ifs, boost::archive::no_header
> ifs.seekg(p);
> ar & restored_o1 & restored_o2 & restored_o3 & restored_p1;
> }
>

Hope this helps,

--
Christian Pfligersdorffer
Software Engineering
www.eos.info

Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net