473,542 Members | 2,057 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.h pp
#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::serializ ation::access;
template<class Archive>
void serialize(Archi ve & ar, const unsigned int version)
{
ar.template register_type<A ttrDouble4dv>() ;
ar.template register_type<A ttrString4dv>() ;

ar & _attrs;
}

public:

vector<Attribut e4dv*_attrs;

Test() {}
~Test() {}

};

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

vector<stringlu t;
lut.push_back(" 2001");
lut.push_back(" 2002");
vector<intlutIn dices;
lutIndices.push _back (0);
lutIndices.push _back(1);
AttrString4dv* attr2 = new AttrString4dv;
attr2->_name = "faultblock ";
attr2->_indices = lutIndices;
attr2->_lut = lut;
tst->_attrs.push_ba ck(attr2);

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

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

return 0;
}

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

#include <vector>
#include <boost/archive/text_iarchive.h pp>
#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::serializ ation::access;
template<class Archive>
void serialize(Archi ve &ar, const unsigned int version)
{
ar & _name;
}

};

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

AttrDouble4dv() {}
~AttrDouble4dv( ) {}

std::vector<dou ble_vals;

private:

// for serialization
friend class boost::serializ ation::access;
template<class Archive>
void serialize(Archi ve &ar, const unsigned int version)
{
// save/load base class info
ar & boost::serializ ation::base_obj ect<Attribute4d v>(*this);

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::serializ ation::access;
template<class Archive>
void serialize(Archi ve &ar, const unsigned int version)
{
// save/load base class info
ar & boost::serializ ation::base_obj ect<Attribute4d v>(*this);

ar & _lut;
ar & _indices;
}
};

#endif // _ATTRIBUTE4DV_H

Nov 9 '07 #1
2 5541
On Nov 8, 9:57 pm, "mkvenkit...@gm ail.com" <mkvenkit...@gm ail.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*********@lis ts.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.boo st.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
3143
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 in a vector. Can I write that vector directly to a binary file?
2
2503
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 files only. Although implementing one is easy, I would like to have one that has already been tested... Thanks,
1
2530
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 struct, in this file there are lot of records i need to loop trough, first time I read the file, the data is fine but for the rest the data is not...
5
6432
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 internal override string ReadString() { stringLength = fileStream.ReadByte(); moInput.Read(byteArrayBuffer, 0, stringLength); return...
2
6609
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 are not a language-supported structure, they are platform-specific. -mod } I'm trying to use boost serialization to serialize/deserialize data to...
0
2094
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: #include <iostream> #include <fstream> #include <string> #include <boost/archive/xml_iarchive.hpp>
31
14432
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 that the read and get functions do not accept float type. What should i do now? Can anybody help me please? Here is the code.. #include...
13
3663
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 consists of structures of type---- struct record { int acountnum; char name; float value;
1
4445
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 {
0
7392
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7330
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7721
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7324
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7670
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
5867
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5246
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3376
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
943
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.