国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 學(xué)院 > 開(kāi)發(fā)設(shè)計(jì) > 正文

ProtoBuf 常用序列化/反序列化API

2019-11-10 20:33:50
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

轉(zhuǎn)自:http://blog.csdn.net/sealyao/article/details/6940245

1、C數(shù)組的序列化和反序列化API

[cpp] view plain copy//C數(shù)組的序列化和序列化API  bool ParseFromArray(const void* data, int size);  bool SerializeToArray(void* data, int size) const;  //使用  void set_people()               {      wp.set_name("sealyao");         wp.set_id(123456);              wp.set_email("sealyaog@Gmail.com");      wp.SerializeToArray(parray,256);  }    void get_people()               {      rap.ParseFromArray(parray,256);      cout << "Get People from Array:" << endl;      cout << "/t Name : " <<rap.name() << endl;      cout << "/t Id : " << rap.id() << endl;      cout << "/t email : " << rap.email() << endl;  }  

2、C++ String的序列化和反序列化API

[cpp] view plain copy//C++string序列化和序列化API  bool SerializeToString(string* output) const;  bool ParseFromString(const string& data);  //使用:  void set_people()               {      wp.set_name("sealyao");         wp.set_id(123456);              wp.set_email("sealyaog@gmail.com");      wp.SerializeToString(&pstring);  }    void get_people()               {      rsp.ParseFromString(pstring);        cout << "Get People from String:" << endl;      cout << "/t Name : " <<rsp.name() << endl;      cout << "/t Id : " << rsp.id() << endl;      cout << "/t email : " << rsp.email() << endl;  }  

3、文件描述符序列化和反序列化API

[cpp] view plain copy //文件描述符的序列化和序列化API   bool SerializeToFileDescriptor(int file_descriptor) const;   bool ParseFromFileDescriptor(int file_descriptor);     //使用:  void set_people()  {      fd = open(path,O_CREAT|O_TRUNC|O_RDWR,0644);      if(fd <= 0){          perror("open");          exit(0);       }         wp.set_name("sealyaog");      wp.set_id(123456);      wp.set_email("sealyaog@gmail.com");      wp.SerializeToFileDescriptor(fd);         close(fd);  }    void get_people()  {      fd = open(path,O_RDONLY);      if(fd <= 0){          perror("open");          exit(0);      }      rp.ParseFromFileDescriptor(fd);      std::cout << "Get People from FD:" << endl;      std::cout << "/t Name : " <<rp.name() << endl;      std::cout << "/t Id : " << rp.id() << endl;      std::cout << "/t email : " << rp.email() << endl;      close(fd);  }  

4、C++  stream 序列化和反序列化API

[cpp] view plain copy//C++ stream 序列化/反序列化API  bool SerializeToOstream(ostream* output) const;  bool ParseFromIstream(istream* input);    //使用:  void set_people()  {      fstream fs(path,ios::out|ios::trunc|ios::binary);      wp.set_name("sealyaog");      wp.set_id(123456);      wp.set_email("sealyaog@gmail.com");      wp.SerializeToOstream(&fs);          fs.close();      fs.clear();  }    void get_people()  {      fstream fs(path,ios::in|ios::binary);      rp.ParseFromIstream(&fs);      std::cout << "/t Name : " <<rp.name() << endl;      std::cout << "/t Id : " << rp.id() << endl;       std::cout << "/t email : " << rp.email() << endl;         fs.close();      fs.clear();  }  
發(fā)表評(píng)論 共有條評(píng)論
用戶(hù)名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 保康县| 禄劝| 泸水县| 江西省| 尼勒克县| 平远县| 新宾| 灌南县| 黑龙江省| 武定县| 敖汉旗| 枣强县| 密山市| 原平市| 金沙县| 黄浦区| 河源市| 绿春县| 古丈县| 陕西省| 长顺县| 丹阳市| 大冶市| 万宁市| 文山县| 得荣县| 赤水市| 泰顺县| 贡山| 重庆市| 水富县| 揭东县| 离岛区| 清河县| 乐业县| 华宁县| 英德市| 洪湖市| 黄梅县| 辽阳县| 昭通市|