|
Boost Users : |
Subject: [Boost-users] Seeking in a serialized file
From: gnu indian (gnuindian_at_[hidden])
Date: 2012-02-20 08:37:52
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:
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();
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;
}
Mridul
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