网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。
一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!
参考官网实验(proto2):https://developers.google.com/protocol-buffers/docs/cpptutorial
结合这位博主已经用proto3修改:https://blog.csdn.net/hailong0715/article/details/52057873?utm_source=blogxgwz5
定义一个ProtoBuf源文件: Person.pro
syntax="proto3";
package tutorial;
message Person
{
string name = 1;
int32 id = 2;
string email = 3;
enum PhoneType
{
MOBILE = 0;
HOME = 1;
WORK = 2;
}
message PhoneNumber
{
string number = 1;
PhoneType type = 2;
}
repeated PhoneNumber phone = 4;
}
message AddressBook
{
repeated Person person =1;
}
可以看到该文件中,有一点类似C语言结构体的风格,自定义数据结构类型并且可以嵌套。Person类型中包含了三个基本类型:name
,id
,email
,同时又包含一个复合类型PhoneNumber
。
编译该文件:
protoc -I=$SRC_DIR --cpp_out=$DST_DIR $SRC_DIR/Person.proto
//这里在当前路径下编译即可
protoc -I=./ --cpp_out=./ ./Person.pro
编译后生成Person.proto,pb.h
和Person.pro.pb.cc
文件,这两个文件包含了Person以及AddressBook的类,以及对应操作类的方法,在你使用者这些类必须包含头文件,最终可执行文件也必须将cc文件也编译进去。
add_person.cpp 展示了如何设置一个联系人的各个字段的信息,并且最终序列化信息到文件中。
//add\_person.cpp
#include "Person.pro.pb.h"
#include <fstream>
#include <iostream>
using namespace std;
void PromptForAddress(tutorial::Person\*);
int main(int argc, char\* argv[])
{
GOOGLE_PROTOBUF_VERIFY_VERSION;
if(2 != argc)
{
//必须指定电话本名称才执行程序
cerr << "Usage: " << argv[0] << " ADDRESS\_BOOK\_FILE" << endl;
return -1;
}
tutorial::AddressBook address_book;
fstream in("ADDRESS\_BOOK\_FILE", ios::binary | ios::in);
if(!in)
{
cerr << "open file ADDRESS\_BOOK\_FILE failed!\n";
return -1;
}
if(!address_book.ParseFromIstream(&in))
{
cerr << "Parse File ADDRESS\_BOOK\_FILE failed!\n";
return -1;
}
in.close();
//增加一个Person,可通过多次调用该接口增加联系人
//具有repeated的属性可通过add\_fieldname方法增加一个属性
PromptForAddress(address_book.add\_person());
fstream out("ADDRESS\_BOOK\_FILE", ios::binary | ios::out | ios::trunc);
if(!address_book.SerializeToOstream(&out))
{
cerr << "Failed to Write Address Book!\n";
return -1;
}
//可选的,回收所有ProtoBuf分配的对象
google::protobuf::ShutdownProtobufLibrary();
return 0;
}
void PromptForAddress(tutorial::Person\* person)
{
cout<<"Enter a Person ID number: ";
int id;
cin >> id;
person->set\_id(id);
/\*忽略CIN的前256个字符,或者忽略CIN的换行符之前的字符,包括换行符
这样的话不会将换行符之前的其他类型的数据保留在输入缓冲中
\*/
cin.ignore(256, '\n');
cout<<"Enter name: ";
getline(cin, \*person->mutable\_name());
cout<< "Enter email address (blank for none): ";
string email;
getline(cin,email);
if(!email.empty())
person->set\_email(email);
while(true)
{
cout<<"Enter a phone number (or leave blank to finish): ";
string number;
getline(cin, number);
if(number.empty())
break;
tutorial::Person::PhoneNumber\* phone_number = person->add\_phone();
phone_number->set\_number(number);
cout<<"Is this a mobile, home, or work phone? ";
string type;
getline(cin, type);
if(type == "mobile")
phone_number->set\_type(tutorial::Person::MOBILE);
else if( type == "home")
phone_number->set\_type(tutorial::Person::HOME);
else if (type == "work")
phone_number->set\_type(tutorial::Person::WORK);
else
{
cout << "Unknown phone type. Using default." << endl;
phone_number->set\_type(tutorial::Person::HOME);
}
}
}
list_person.cpp展示了如何从文件中读取ProtoBuf序列化的信息
//list\_person.cpp
#include "Person.pro.pb.h"
#include <iostream>
#include <fstream>
using namespace std;
void ListPeople(const tutorial::AddressBook& address_book);
int main(int argc, char \* argv[])
{
if(2!=argc)
{
cerr << "Usage: " << argv[0] << " ADDRESS\_BOOK\_FILE" << endl;
return -1;
}
fstream in("ADDRESS\_BOOK\_FILE", ios::in | ios::binary);
tutorial::AddressBook address_book;
if(!address_book.ParseFromIstream(&in))
{
cerr << "Parse Input File failed!"<< endl;
return -1;
}
ListPeople(address_book);
google::protobuf::ShutdownProtobufLibrary();
return 0;
}
void ListPeople(const tutorial::AddressBook& address_book)
{
//fieldName\_size方法返回具有repeated属性的个数
for(int i=0; i< address_book.person\_size();i++)
{
const tutorial::Person& person = address_book.person(i);
cout<<"Person ID: "<<person.id();
cout<<"Name: "<<person.name();
cout<<"Email: "<<person.email();
for(int j=0; j< person.phone\_size();j++)
{
const tutorial::Person::PhoneNumber& phone_number = person.phone(j);
switch(phone_number.type())
{
case tutorial::Person::MOBILE:
cout << " Mobile phone #: ";
![img](https://img-blog.csdnimg.cn/img_convert/855ed0af0486b4952c08739d031af155.png)
![img](https://img-blog.csdnimg.cn/img_convert/68440d55925f203f89f9fe4b58fc6456.png)
**既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,涵盖了95%以上C C++开发知识点,真正体系化!**
**由于文件比较多,这里只是将部分目录截图出来,全套包含大厂面经、学习笔记、源码讲义、实战项目、大纲路线、讲解视频,并且后续会持续更新**
**[如果你需要这些资料,可以戳这里获取](https://bbs.csdn.net/topics/618668825)**
(img-8ffpStRb-1715696330442)]
**既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,涵盖了95%以上C C++开发知识点,真正体系化!**
**由于文件比较多,这里只是将部分目录截图出来,全套包含大厂面经、学习笔记、源码讲义、实战项目、大纲路线、讲解视频,并且后续会持续更新**
**[如果你需要这些资料,可以戳这里获取](https://bbs.csdn.net/topics/618668825)**