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;
    }

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.