In below code i am writing file contents in backup serialize file through boost serialization library, but what i observed that every time i need to store all the data in file then only i would be able to read the entire data. so in case if i perform single write operation and just try to get the store data in backup file then not able to read anything stored in backup file as it looks like old contents were not present in the file . After executing entire code i am able to read the serialize map from file but while performing read operation only without write not able to read anything. below is the commented code snippet for write operation. please suggest if there is something wrong in the code .

Note: std::ofstream ss("backup",ios::app); doesn't give me desire result.

commented portion of the code of writefile Api

 /*obj.writefile("wasim",23);
    obj.writefile("available",12);
    obj.writefile("jack",32);
    obj.writefile("john",37);
    obj.writefile("hate",11);
    obj.writefile("kk",37);*/
    obj.writefile("abc",39); 
    obj.readfile();
    ret_value=obj.parse_map();
    cout<<ret_value;

    Entire code snippet:

#include <map>
#include <sstream>
#include <string>
#include <boost/serialization/map.hpp>
#include <boost/archive/text_iarchive.hpp>
#include <boost/archive/text_oarchive.hpp>
#include <fstream>
#include <iostream>
#include <boost/archive/binary_oarchive.hpp>
#include <boost/archive/binary_iarchive.hpp>
using namespace std;
enum RESULT_T
            {

            ret_error   =-1,
            ret_success = 0

            } ;

class object_model
{
private:
    friend class boost::serialization::access;
    template<class Archive>
    void serialize(Archive & ar, const unsigned int version)
    {
        ar & mymap;

    }

    map<string,int> mymap;

public:
         object_model(){

     };

     void writefile (string key,int value);
     map<string, int> readfile();
     RESULT_T parse_map();
     int deserialize_validation();

};

 RESULT_T object_model::parse_map()
{

    auto new_map = readfile();
    for (auto itr: new_map)
               {

                cout<<itr.first <<":"<<itr.second<<endl;

               }

return ret_success;
}
void object_model::writefile( string key, int value)
{

    mymap[key] = value;
    std::ofstream ss("backup");
    boost::archive::text_oarchive oarch(ss);
    oarch << mymap;

}

map<string, int>  object_model::readfile()
{
    std::map<string,int> new_map;
       {
       std::ifstream ifs("backup",ios::binary);
       boost::archive::text_iarchive ia(ifs);
       ia >> new_map;
       }

       return new_map;

}
int main()
{
    RESULT_T ret_value;
    object_model obj;
    obj.writefile("hate",11);
    obj.writefile("wasim",23);
    obj.writefile("available",12);
    obj.writefile("jack",32);
    obj.writefile("john",37);
    obj.writefile("kk",37);

    obj.readfile();
    ret_value=obj.parse_map();
    cout<<ret_value;
    }

Dani AI

Generated

Quick diagnosis for (and answering ’s format question): Boost’s text_i/oarchive writes a complete archive (header + object graph). Opening the same file and appending another archive produces two archives back‑to‑back, it does not “merge” maps. Also be careful to open streams in the correct mode (text vs binary) consistently — mixing them (or forgetting to flush/close properly) will corrupt what you try to read later.

Practical fixes (pick one):

  • Keep one canonical map (recommended). On start, read the file into your map (handle missing/empty file), mutate the map in memory, then overwrite the file (open with truncation) and serialize the whole map. This is simple, robust and avoids dealing with multiple archive headers. For crash safety, write to a temporary file then atomically rename it.

  • If you really need append semantics (log of records), serialize individual records (a pair or small struct) and append them. When reading, open the file and repeatedly construct an iarchive to extract the next record until EOF (catch archive exceptions for clean termination). That way you treat the file as a sequence of independent serialized items.

Example (append-one-record / read-all pattern — use binary archives consistently):

std::ofstream ofs("backup.dat", std::ios::binary | std::ios::app);
{
  boost::archive::binary_oarchive oa(ofs);
  oa << std::make_pair(std::string("k"), 42);
}

// reading:
std::ifstream ifs("backup.dat", std::ios::binary);
while (ifs && !ifs.eof()) {
  try {
    boost::archive::binary_iarchive ia(ifs);
    std::pair<std::string,int> p;
    ia >> p;
    // merge p into map
  } catch (boost::archive::archive_exception&) {
    break;
  }
}

Extra tips: don’t mix text/binary modes; use the same archive type and stream flags for write and read. If you choose the overwrite approach, prefer writing to a temp file and renaming to avoid partial/corrupt files after crashes. Also check small issues in the posted code (typos in the serialize template line will cause compile errors).

Recommended Answers

All 2 Replies

Any particular reason why you are using the Boost serialization library? Also, what is the raw format that Boost puts the data into? XML? TCL? Proprietary?

it is simple txt file only as text_archive is used .I need to persist the objects and read them back . I tried std::ofstream ss("backup",ios::out|ios::app); but it is not working properly.Not sure why append is not happening .

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.