Seeking in a serialized file

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

gnu indian 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:
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
This is not supported by any current archives. It has likely been implemented by someone who concatonates separate serializations into files, and this might be an interesting case to add to the examples, but so far no one has done this. Robert Ramey

gnu indian
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
participants (3)
-
gnu indian
-
Pfligersdorffer, Christian
-
Robert Ramey