日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

protobuf入门教程(三):常用序列化/反序列化接口

發布時間:2024/4/11 编程问答 23 豆豆
生活随笔 收集整理的這篇文章主要介紹了 protobuf入门教程(三):常用序列化/反序列化接口 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

C數組的序列化和反序列化

#include "addressbook.pb.h" #include <iostream> using namespace std;/* //C數組的序列化和序列化API //在/usr/local/include/google/目錄下,查找包含"SerializeToArray"所有的文件,同時打印所在行 //sudo grep "SerializeToArray" -r /usr/local/include/google/ -nbool SerializeToArray(void* data, int size) const; //序列化 bool ParseFromArray(const void* data, int size); //反序列化 */ char buf[1024]; int len;void set_person() {Person obj;obj.set_name("mike");obj.set_id(1);*obj.mutable_email() = "xxx@qq.com"; //obj.set_email("xxx@qq.com");len = obj.ByteSize(); //獲取長度cout << "len = " << len << endl;obj.SerializeToArray(buf, len);//序列化,obj成員保存在buf中 }void get_person() {Person obj;obj.ParseFromArray(buf, len); //反序列化,buf的內容設置給obj的成員cout << "name = " << obj.name() << endl;cout << "id = " << obj.id() << endl;cout << "email = " << obj.email() << endl; }int main() {// Verify that the version of the library that we linked against is// compatible with the version of the headers we compiled against.GOOGLE_PROTOBUF_VERIFY_VERSION;set_person(); //序列化get_person(); //反序列化// Optional: Delete all global objects allocated by libprotobuf.google::protobuf::ShutdownProtobufLibrary();return 0; }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52

C++ String的序列化和反序列化

//bool SerializeToString(string* output) const; //bool ParseFromString(const string& data); string str; //全局變量void set_person() {Person obj;obj.set_name("mike");obj.set_id(1);obj.set_email("xxx@qq.com");//*obj.mutable_email() = "xxx@qq.com";obj.SerializeToString(&str); //序列化,obj成員的內容設置給str }void get_person() {Person obj;obj.ParseFromString(str); //反序列化, str內容設置給obj的成員cout << "name = " << obj.name() << endl;cout << "id = " << obj.id() << endl;cout << "email = " << *obj.mutable_email() << endl; }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25

文件描述符序列化和反序列化

//bool SerializeToFileDescriptor(int file_descriptor) const; //bool ParseFromFileDescriptor(int file_descriptor); void set_person() {Person obj;obj.set_name("mike");obj.set_id(1);obj.set_email("xxx@qq.com");//*obj.mutable_email() = "xxx@qq.com";//O_CREAT: 新建文件, O_TRUNC:清空文件,O_RDWR:讀寫int fd = open("./pb.xxx", O_CREAT | O_TRUNC | O_RDWR, 0644);if (fd <= 0){perror("open");exit(0);}obj.SerializeToFileDescriptor(fd); //序列化,obj成員的內容寫入fd所關聯的文件中close(fd); //關閉文件 }void get_person() {int fd = open("./pb.xxx", O_RDONLY); //O_RDONLY: 只讀方式if (fd <= 0){perror("open");exit(0);}Person obj;obj.ParseFromFileDescriptor(fd); //反序列化, fd文件內容設置給obj的成員close(fd); //關閉文件cout << "name = " << obj.name() << endl;cout << "id = " << obj.id() << endl;cout << "email = " << *obj.mutable_email() << endl; }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41

C++ stream 序列化和反序列化

//bool SerializeToOstream(ostream* output) const; //bool ParseFromIstream(istream* input); void set_person() {Person obj;obj.set_name("mike");obj.set_id(1);obj.set_email("xxx@qq.com");//*obj.mutable_email() = "xxx@qq.com";fstream output("pb.xxx", ios::out | ios::trunc | ios::binary);bool flag = obj.SerializeToOstream(&output);//序列化if (!flag){cerr << "Failed to write file." << endl;return;}output.close();//關閉文件 }void get_person() {Person obj;fstream input("./pb.xxx", ios::in | ios::binary);obj.ParseFromIstream(&input); //反序列化input.close(); //關閉文件cout << "name = " << obj.name() << endl;cout << "id = " << obj.id() << endl;cout << "email = " << *obj.mutable_email() << endl; }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34

本教程源代碼下載地址:http://download.csdn.net/detail/tennysonsky/9884275

本文轉自:http://blog.csdn.net/sealyao/article/details/6940245

總結

以上是生活随笔為你收集整理的protobuf入门教程(三):常用序列化/反序列化接口的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。