zoukankan      html  css  js  c++  java
  • 【原创】C++中对象的序列化

      1、对象序列化

        对象的序列化是指将对象的状态信息转换为可以存储或者传输的形式的过程。对象的反序列化是与序列化相反的过程。

        在序列化期间,对象将其当前的状态写入到临时或者永久性的存储区,可以通过从存储区读取或者反序列化对象的状态,重新创建该对象。

        通过序列化,可以将对象从一个应用域发送到另一个应用域中,或者通过网络连接将对象数据发送到另一台主机。

        博文中将对象序列化到文件中。

      2、C++下对象序列化的实现

        1)将一个类的一个对象序列化到文件

          这是最简单的情况,一个类中只定义一个数据成员。

        

      1 //Srlz1.cpp: 将一个类的一个对象序列化到文件
      2 #include <iostream>
      3 #include <fcntl.h>
      4 #include <vector>
      5 #include <stdio.h>
      6 using namespace std;
      7 
      8 //定义类CA
      9 //数据成员:int x;
     10 //成员函数:Serialize:进行序列化函数
     11 //             Deserialize反序列化函数
     12 //             Show:数据成员输出函数
     13 class CA
     14 {
     15     private:
     16         int x;  //定义一个类的数据成员。
     17 
     18     public:
     19         CA()    //默认构造函数
     20         {
     21             x = 0;
     22         }
     23         CA(int y):x(y)    //定义构造函数,用初始化列表初始化数据成员
     24         {
     25         }
     26         virtual ~CA()    //析构函数
     27         {
     28         }
     29   public:
     30         //序列化函数:Serialize
     31         //成功,返回0,失败,返回0;
     32         int Serialize(const char* pFilePath) const
     33         {
     34             int isSrlzed = -1;
     35             FILE* fp;   //define a file pointer
     36             //以读写方式打开文件,并判断是否打开;
     37        if ((fp = fopen(pFilePath, "w+")) == NULL)     
     38            {
     39                 printf("file opened failure
    ");    
     40                 return -1;  //若打开失败,则返回-1;
     41             }
     42             //调用fwrite函数,将对象写入文件;
     43        isSrlzed = fwrite(&x, sizeof(int), 1, fp); 
     44 
     45        //判断写入是否成功;
     46        if ((-1 == isSrlzed) || (0 == isSrlzed))    
     47             {
     48                 printf("Serialize failure
    ");
     49                 return -1;  //若写入失败,则返回-1;
     50             }
     51             if(fclose(fp) != 0) //关闭文件
     52             {
     53                 printf("Serialize file closed failure.
    ");
     54                 return -1;
     55             }
     56             printf("Serialize succeed.
    ");
     57             return 0;   //序列化成功,返回0;
     58         }
     59         
     60         //反序列化函数:
     61         //成功,返回0,失败,返回-1;
     62         int Deserialize(const char* pFilePath)
     63         {
     64             int isDsrlzed = -1;
     65             FILE* fp;
     66             //以读写方式打开文件,并判断是否打开;
     67             if ((fp = fopen(pFilePath, "r+")) == NULL)
     68             {
     69                 printf("file opened failure.
    ");
     70                 return -1;
     71             }
     72                //调用fread函数,读取文件中的对象 ;
     73        isDsrlzed = fread(&x, sizeof(int), 1, fp);
     74        //判断是否成功读入
     75             if ((-1 == isDsrlzed)||(0 == isDsrlzed))
     76             {
     77                 printf("Deserialize failure.
    ");
     78                 return -1;    //若读取失败,则返回-1;
     79             }
     80             if(fclose(fp) != 0)
     81             {
     82                 printf("Deserialize file closed failure.
    ");
     83                 return -1;
     84             }
     85             printf("Deserialize succeed.
    ");
     86             return 0;        //反序列化成功,则返回0;
     87      }
     88         
     89 
     90         //成员对象输出显示函数
     91         void Show()
     92         {
     93             cout<< "in Show():"<< x << endl;
     94         }
     95 };
    View Code

        以上代码定义了class CA,包括:

        数据成员:x;

        序列化函数:Serialize();

        反序列化函数:Derialize();

        数据成员输出函数:Show();

        main函数:

        

     1 int main(int argc, char const *argv[])
     2 {
     3   CA as(100);    //定义一个类对象,并初始化;
     4 
     5     //调用序列化函数,将对象as序列化到文件data.txt中;
     6   as.Serialize("data.txt");
     7 
     8   CA ad;        //定义一个类对象,用来记录反序列化对象
     9   //调用反序列化函数,将文件data.txt中的对象反序列化到ad对象;
    10   ad.Deserialize("data.txt");
    11 
    12   ad.Show();    //调用输出显示函数;
    13 
    14   return 0;
    15 
    16 }

       

  • 相关阅读:
    Django(app的概念、ORM介绍及编码错误问题)
    Django(完整的登录示例、render字符串替换和redirect跳转)
    Construct Binary Tree from Preorder and Inorder Traversal
    Single Number II
    Single Number
    Binary Tree Level Order Traversal II
    Binary Tree Level Order Traversal
    Binary Tree Zigzag Level Order Traversal
    Recover Binary Search Tree
    Add Binary
  • 原文地址:https://www.cnblogs.com/pxtj/p/3378160.html
Copyright © 2011-2022 走看看