How to use C++ Boots installation and map serialization?
Friends I tried to store map data into file this way but not able to use it.
I read many pages from boots help but it is not clear to me so if you know any link which is showing step by step information about how to use boots kindly post here.
First of all installing boots is very hard.
I done following for boost installation:
1. Download boost zip and unzip
2. Set path for gcc
3. Run Bootstrap.bat gcc
4. Run B2.exe toolset=gcc
5. Add boots folder in CodeBlocks Settings Compiler Search directories Compiler tabs
Q.1. Do I need to anything else to install and run boost?
Q.2. How I can write and read map in to file?
I got this error:
C:\boost_1_60_0\boost\serialization\access.hpp|116|error: 'class Account' has no member named 'serialize'|
Here is my dummy code.
Code:
// This is dummy code
// Account.h is something like this
#include <iostream>
#include <iomanip>
using namespace std;
class Account
{
private:
static int lastAcNo;
static float sbRate;
string name;
string acType;
public:
Account()
{
}
void create(string s)
{
}
void print()
{
}
};
// Bank.h something like this
#include <iostream>
#include <fstream>
#include <map>
#include "Account.h"
#include <boost/archive/text_iarchive.hpp>
#include <boost/archive/text_oarchive.hpp>
using namespace std;
int Account::lastAcNo = 0;
float Account::sbRate = 0;
class Bank
{
private:
typedef map<int, Account> bankMap;
bankMap data;
string bankName;
int lastAcNo;
friend class boost::serialization::access;
template<class Archive>
void serialize(Archive & ar, const unsigned int version)
{
ar & data;
}
void writeAccountData()
{
// here i want to write entire map to file or just a object whichever is easy
// but i don't know steps for using boots
Account ac;
ofstream dataFile;
dataFile.open("BankData.dat", ios::out|ios::binary);
boost::archive::text_oarchive bDataFile(dataFile);
cout << endl << "Writing data..." << endl;
for(bankMap::iterator i = data.begin(); i != data.end(); ++i)
{
ac = i->second;
bDataFile << ac;
//first i used normal storing but it is also not working
//dataFile.write(reinterpret_cast<char *>(&ac), sizeof(ac));
}
dataFile.close();
cout << endl << "Finish writing." << endl;
}
void readAccountData()
{
// here i want to read entire map or object
fstream dataFile;
dataFile.open("BankData.dat",ios::in|ios::binary);
boost::archive::text_iarchive bDataFile(dataFile);
cout << endl << "Reading data..." << endl;
if(dataFile.fail())
{
Account::setLastAcNo(101);
return;
}
else
{
Account ac;
dataFile.seekg(0,ios::end);
int size = dataFile.tellg();
int totAc = size/sizeof(ac);
dataFile.seekg(0,ios::beg);
int tAcNo;
for(int i=0; i<totAc; i++)
{
//first tried to read like this but not working
//dataFile.read(reinterpret_cast<char *>(&ac), sizeof(ac));
bDataFile >> ac;
data.insert(make_pair(ac.getAcNo(),ac));
tAcNo = ac.getAcNo();
}
Account::setLastAcNo(++tAcNo);
cout << endl << "Finish reading." << endl;
}
dataFile.close();
}
public:
Bank()
{
readAccountData();
}
~Bank()
{
writeAccountData();
}
}
//main.cpp file something like this
int main()
{
Bank b;
b.menu();
return 0;
}
Re: How to use C++ Boots installation and map serialization?
It's called *BOOST*. Not "BOOTS".
Not 100% sure but: You wrote the serialization code for your bank, which serializes the map of accounts. But how does it know how to serialize the accounts themselves? You should probably also add a serialize function to your Account class.
Re: How to use C++ Boots installation and map serialization?
Code:
dataFile.write(reinterpret_cast<char *>(&ac), sizeof(ac));
This will only work if all the data is stored as part of the class (eg int, float etc). Where a dynamic type is used (eg string) this doesn't work as the data is stored in memory outside of the class. The class only holds a pointer to the data.
Code:
Account ac;
dataFile.seekg(0,ios::end);
int size = dataFile.tellg();
int totAc = size/sizeof(ac);
dataFile.seekg(0,ios::beg);
This code won't do what is expected if Account is written to the file properly because the size of name etc (a string) is variable - so the size of the data written for each Account record could be different.
For stream insertion/extraction for a user defined struct/class you need to override the operator<< (and operator>>) for the required class.struct. Is this part of the code you haven't included?
Re: How to use C++ Boots installation and map serialization?
Thanks monarch_dodra
Yes Boost it was my typing mistake.
How i can serialize Account class for that i have to write each variable in Account serialize function as i wrote data in bank class?
Re: How to use C++ Boots installation and map serialization?
Thank you 2kaud
I tried first code but i am using string in Account class so not working.
Can any one able to tell how i can easily write code for this?