Hey I am having an issue storing string in binary format than reading them back.
here is the code.
Code:
#include <iostream>
#include <conio.h>
#include <vector>
#include <string.h>
#include <map>
#include <fstream>
using namespace std;
typedef map<int,string> ZMAP;
typedef std::pair<int, string> Vp;
ZMAP daMap;
ZMAP Map2;
int main()
{
cout<<"1(Write) 2(Read): ";
int c;
cin>>c;
if(c == 1)
{
fstream ff;
daMap.insert(ZMAP::value_type(0,"bob"));
daMap.insert(ZMAP::value_type(1,"bob1"));
daMap.insert(ZMAP::value_type(2,"bob2"));
ff.open("Test.file", ios::out | ios::binary);
vector<Vp> v;
v.reserve(daMap.size());
v.insert(v.begin(),daMap.begin(),daMap.end());
vector<Vp>::size_type sz = v.size();
ff.write(reinterpret_cast<char *>(&sz),sizeof(sz));
ff.write(reinterpret_cast<char *>(&v[0]),v.size() * sizeof(v[0]));
ff.close();
}else{
fstream fs;
fs.open("Test.file", ios::in | ios::binary);
vector<Vp>::size_type szv;
fs.read(reinterpret_cast<char *>(&szv),sizeof(szv));
vector<Vp> z(szv);
fs.read(reinterpret_cast<char *>(&z[0]),z.size() * sizeof z[0]);
Map2.insert(z.begin(),z.end());
for(ZMAP::iterator fsz = Map2.begin(); fsz!= Map2.end();++fsz)
{
cout<<(*fsz).first<<endl<<(*fsz).second<<endl;
}
}
cout<<"Done"<<endl;
getch();
}
The Output that i get when reading the data back in is...
Code:
0
e(2
1
(2
2
(2
Done
Any advise on how to fix this is greatly appricated.
The goal here is to take the Map data put it into a vector which is written into binary, since this is what was suggested by a few sites.
Thanks again for any advice.