CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Jul 2001
    Posts
    27

    Question 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;
    }
    Last edited by P_N_Data_System; January 25th, 2016 at 06:23 AM.

  2. #2
    Join Date
    Jun 2009
    Location
    France
    Posts
    2,513

    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.
    Is your question related to IO?
    Read this C++ FAQ article at parashift by Marshall Cline. In particular points 1-6.
    It will explain how to correctly deal with IO, how to validate input, and why you shouldn't count on "while(!in.eof())". And it always makes for excellent reading.

  3. #3
    2kaud's Avatar
    2kaud is online now Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    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.

    Code:
    bDataFile << ac;
    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?
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  4. #4
    Join Date
    Jul 2001
    Posts
    27

    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?

  5. #5
    Join Date
    Jul 2001
    Posts
    27

    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?

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured