0

The task I tried to accomplish was a set of strings which have to be stored into a std::set and then save it to a file with a number assigned to each set. and I managed to accomplish it by below code using some boost functions

void WriteToFile()
{
   std::set<std::string> ptotoList = {"John", "Kelly", "Amanda", "Kim"};
   std::set<std::string> etotoList = {"Jo", "Ke", "Am", "Ki"};
   const std::string solMsg = SerializeCutoverListMsg (ptotoList,etotoList);
   std::string msgToBeSaved = 1 + ":" + solMsg;
   std::ofstream outStream (FileName.string().c_str());
   outStream << boost::algorithm::join(msgsToBeSaved, "\n") << endl;

}

string SerializeCutoverListMsg(const std::set& pList, const const std::set&  eList)
{
   return boost::algorithm::join(eList, ",") + ";" + boost::algorithm::join(pList, ",");
}

Using this function the Output I generate resembles like below and I save it in a file, I have a number assigned to each set

  1:John,Kelly,Amanda,Kim;Jo,Ke,Am,Ki

And to Read the same data i used the below code

 void ReadFromFile()
 {
 std::ifstream wssmAssetListFile(someFilename.c_str());
 string msgLine;
 while ( getline (wssmAssetListFile, msgLine) )
   {
        boost::algorithm::split_regex( result, msgLine, boost::regex( ":" ) ) ;   
         const string& groupId = result[0];
        ---------
   }
 }

Now the data that belonged to a set changed to a map , and the data that has to be read and written looks like below

struct test
{
  string a;
  string b;
}
void WriteToFile()
{
  std::map<int, test> eList,pList ;
  eList["John"] = {"dummy", "dummy1"};
  pList["Jo"] = {"dummy", "dummy1"};
   and so on...
}

}

I would need help how to write the data like below and read it like I did above just for set

1:John{"dummy", "dummy1"},Kelly{"dummy", "dummy1"},Amanda{"dummy", "dummy1"},Kim{"dummy", "dummy1"};Jo{"dummy", "dummy1"},Ke{"dummy", "dummy1"},Am{"dummy", "dummy1"},Ki{"dummy", "dummy1"}

Initially we had set of strings but now each string contains a structure and thus I had to use a map as each value in the set had its corresponding values

Appreciate your help on this

LearningCpp
  • 972
  • 12
  • 29
  • I would split by the record delimiter `;` first. Create a list of "records" and then iterate over the records, split on `:`, and then construct the map from the "record" value. – Ingenioushax Mar 23 '17 at 16:47
  • @Ingenioushax you don't have to split anything by; that's isn't whts required it's a set changed to hash now and dealing with it is a bit deep – LearningCpp Mar 23 '17 at 16:50
  • Ah. Misunderstood the question. – Ingenioushax Mar 23 '17 at 16:52

0 Answers0