473,287 Members | 1,582 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,287 software developers and data experts.

Boost serialization: Problem reading strings from binary archive

Hello,

I hope this is the right place to post a question on Boost. If not,
please let me know where I can post this message and I will do so.

I am having a strange problem with std::string as I am trying to read
from a binary archive using Boost serialization. I am new to this, and
it is possible that I have not understood the usage. In the code
below, the string "faultblock" seems to be causing the problem. The
code crashes in the ia & tst line which is reading back the archive -
the stack indicates that it is trying to read an invalid string.

I have tried to minimize the code I need to post, but sorry if this is
still too much.

I'd appreciate any help with this. I am using Boost 1.34.0 on windows
XP.

Best Regards

Mahesh
//
// test.cpp
//
#include <iomanip>
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <boost/archive/text_iarchive
..hpp>
#include <boost/archive/text_oarchive.hpp
#include <boost/archive/binary_oarchive.hpp>
#include <boost/archive/binary_iarchive.hpp>
#include <boost/serialization/base_object.hpp>
#include <boost/serialization/utility.hpp>
#include <boost/serialization/list.hpp>

#include "Attribute4dv.h"

using namespace std;

class Test
{
private:
friend class boost::serialization::access;
template<class Archive>
void serialize(Archive & ar, const unsigned int version)
{
ar.template register_type<AttrDouble4dv>();
ar.template register_type<AttrString4dv>();

ar & _attrs;
}

public:

vector<Attribute4dv*_attrs;

Test() {}
~Test() {}

};

int main(int argc, char* arv[])
{
// save data to archive
{
Test* tst = new Test;
vector<doubledVals;
dVals.push_back(1.1);
dVals.push_back(2.2);
AttrDouble4dv* attr1 = new AttrDouble4dv;
attr1->_name = "attr1";
attr1->_vals = dVals;
tst->_attrs.push_back(attr1);

vector<stringlut;
lut.push_back("2001");
lut.push_back("2002");
vector<intlutIndices;
lutIndices.push_back (0);
lutIndices.push_back(1);
AttrString4dv* attr2 = new AttrString4dv;
attr2->_name = "faultblock";
attr2->_indices = lutIndices;
attr2->_lut = lut;
tst->_attrs.push_back(attr2);

const Test* tst2 = tst;
std::ofstream ofs("test.bin");
boost::archive::binary_oarchive oa(ofs);
oa & tst2;
}

// read data from archive
{
Test* tst;
std::ifstream ifs("test.bin", std::ios::binary);
boost::archive::binary_iarchive ia(ifs);
ia & tst;
int j = 0;
}

return 0;
}

//
// Attribute4dv.h
//
#ifndef _ATTRIBUTE4DV_H
#define _ATTRIBUTE4DV_H

#include <vector>
#include <boost/archive/text_iarchive.hpp>
#include <boost/serialization/base_object.hpp>
#include <boost/serialization/string.hpp>
#include <boost/serialization/vector.hpp>

// Base class
class Attribute4dv
{
public:

Attribute4dv() {}
virtual ~Attribute4dv() {}

// name of attribute
std::string _name;

private:

// for serialization
friend class boost::serialization::access;
template<class Archive>
void serialize(Archive &ar, const unsigned int version)
{
ar & _name;
}

};

// double attributes
class AttrDouble4dv : public Attribute4dv
{
public:

AttrDouble4dv() {}
~AttrDouble4dv() {}

std::vector<double_vals;

private:

// for serialization
friend class boost::serialization::access;
template<class Archive>
void serialize(Archive &ar, const unsigned int version)
{
// save/load base class info
ar & boost::serialization::base_object<Attribute4dv>(*t his);

ar & _vals;
}
};

// string attributes
class AttrString4dv : public Attribute4dv
{
public:

AttrString4dv() {}
~AttrString4dv() {}

// a lookup table (LUT) of strings
std::vector<std::string_lut;
// indices to the LUT
std::vector<int_indices;

private:

// for serialization
friend class boost::serialization::access;
template<class Archive>
void serialize(Archive &ar, const unsigned int version)
{
// save/load base class info
ar & boost::serialization::base_object<Attribute4dv>(*t his);

ar & _lut;
ar & _indices;
}
};

#endif // _ATTRIBUTE4DV_H

Nov 9 '07 #1
2 5507
On Nov 8, 9:57 pm, "mkvenkit...@gmail.com" <mkvenkit...@gmail.com>
wrote:
Hello,

I hope this is the right place to post a question on Boost. If not,
please let me know where I can post this message and I will do so.
This page has more info
http://lists.boost.org/mailman/listinfo.cgi/boost-users

You can send email to bo*********@lists.boost.org
and have this posted there.

Brian Wood
Ebenezer Enterprises
www.webebenezer.net

Nov 9 '07 #2
On Nov 9, 9:13 am, c...@mailvault.com wrote:
....
You can send email to boost-us...@lists.boost.org
and have this posted there.

Brian Wood
Ebenezer Enterpriseswww.webebenezer.net

Thanks - I just emailed the group.

Mahesh

Nov 9 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: nightflyer | last post by:
Hi all, [code snippet appended at the end.) my question: A class has a few string variables with not know length at design time. Now I declare lets say a 1000 of those classes and put them...
2
by: eyal.susser | last post by:
Hi, I'm using the Boost serialization library. Iwant to serialize classes into simple buffers on RAM. There doesn't seem to be a suitable archiver. Am I missing it? The ones I saw were for...
1
by: Jón Sveinsson | last post by:
Hello everyone I have been able to read data from binary files to filestrean, the data in the files are structured, what I'm trying to do is to loop through the binary files and add data to my...
5
by: rnorthedge | last post by:
I am working on a code library which needs to read in the data from large binary files. The files hold int, double and string data. This is the code for reading in the strings: protected...
2
by: smith4894 | last post by:
{ not sure you're aware of that but there are the newsgroups for all major operating systems. you might want to try asking in the forum 'comp.os.linux.development.apps', since memory-mapped files...
0
by: Abhishek Padmanabh | last post by:
I have been trying out boost's serialization library for the past few days. And I have come across a problem serializing a class that has a reference member. The code is posted as below: ...
31
by: tophandasa | last post by:
Hi all, I'm having a trouble reading a binary file as float values.I have to read the data in binary mode, then read every four bytes into a float variable. I have done my search, but i found out...
13
by: swetha | last post by:
HI Every1, I have a problem in reading a binary file. Actually i want a C program which reads in the data from a file which is in binary format and i want to update values in it. The file...
1
by: MindWrapper | last post by:
boost serialization of polymorph classes from DLLs Folks, Let's consider following code -------------------------------- // base.h // abstract base class class IBase {
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.