C ++中复杂结构的序列化 [英] Serialization of complex structures in C++

查看:103
本文介绍了C ++中复杂结构的序列化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在C ++中序列化一组结构。这对于除了包含在我的结构中的向量之外的所有数据都很好。我可以将数据写入磁盘,然后将所有数据读回内存。唯一的问题是当我尝试访问一个元素的矢量我得到一个分段的错误。我的代码如下。

  int main {
struct Student Student;
strcpy(one.FullName,Ernestine Waller);
strcpy(one.CompleteAddress,824 Larson Drv,Silver Spring,MD 20910);
one.Gender ='F';
one.LivesInASingleParentHome = true;
one.grades.push_back(80);
one.grades.push_back(90);

ofstream ofs(fifthgrade.ros,ios :: binary);
ofs.write((char *)& one,sizeof(one));
ofs.close();
}

程序从磁盘读取

  int main {
struct Student * two =(struct Student *)malloc(sizeof(struct Student));
ifstream ifs(fifthgrade.ros,ios :: binary);
// cout<< Size of struct:<尺寸< endl;
ifs.read((char *)two,sizeof(struct Student));
cout<< 学生姓名:< two> FullName<< endl;
cout<< 第一级:<二级[0]<< endl;
cout<< 二年级:<二级[1]<< endl;
ifs.close();
return 0;
}


解决方案

复制其中已存储一个的内存的连续区域,并将其写入磁盘。这将适用于简单的数据类型(POD在C ++ jargan)。向量的问题是向量是具有指向存储器的其他区域的指针的复杂对象。当将一个反序列化为两个因此您的分段错误时,这些其他区域的内存不存在。 p>

不幸的是没有快捷方式,需要写一些形式的自定义序列化代码来完成工作。



由于有人已经提到 Boost序列化可能会有所帮助。或者展开自己的。


I'm trying to serialize a set of structs in C++. This works great for all data except a vector contained in my struct. I can write the data to disk, and then read all data back into memory. The only problem is when I try to access an element of the vector I get a segmentation fault. My code is below. Any help is greatly appreciated.

Program to write to disk

int main {
  struct Student one;
  strcpy(one.FullName, "Ernestine Waller");
  strcpy(one.CompleteAddress, "824 Larson Drv, Silver Spring, MD 20910");
  one.Gender = 'F';
  one.LivesInASingleParentHome = true;
  one.grades.push_back(80);
  one.grades.push_back(90);

  ofstream ofs("fifthgrade.ros", ios::binary);
  ofs.write((char *)&one, sizeof(one));
  ofs.close();
}

Program to read from disk

 int main {
    struct Student *two = (struct Student *) malloc(sizeof(struct Student));    
    ifstream ifs("fifthgrade.ros", ios::binary);
    //cout << "Size of struct: " << size << endl;
    ifs.read((char *)two, sizeof(struct Student));
    cout << "Student Name: " << two->FullName << endl;
    cout << "First Grade: " <<  two->grades[0] << endl;
    cout << "Second Grade: " << two->grades[1] << endl;
    ifs.close();
    return 0;
 }

解决方案

What you are doing is taking copying a contiguous region of memory in which you have stored one and wrote it to disk. This will work fine for simple data types (POD in C++ jargan). The problem with vectors is that a vector is a complex object with pointers to other regions of memory. These other regions of memory don't exist when you de-serialize one into two hence your segmentation fault.

Unfortunately there is no shortcut, some form on custom serialization code will need to be written to get the job done.

As some have already mentioned Boost Serialization may help. Alternatively roll out your own.

这篇关于C ++中复杂结构的序列化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆