protobuf使用方法
2015-08-11 18:38
393 查看
person.proto文件
[cpp] view
plaincopy
message Person{
required string name = 1;
required int32 age = 2;
optional string email = 3;
enum PhoneType{
MOBILE = 1;
HOME = 2;
WORK = 3;
}
message Phone{
required int32 id = 1;
optional PhoneType type = 2 [default = HOME];
}
repeated string phoneNum = 4; //对应于cpp的vector
}
安装好protoc以后,执行protoc person.proto --cpp_out=. 生成 person.pb.h和person.pb.cpp
写文件(write_person.cpp):
[cpp] view
plaincopy
#include <iostream>
#include "person.pb.h"
#include <fstream>
#include <string>
using namespace std;
int main(){
string buffer;
Person person;
person.set_name("chemical");
person.set_age(29);
person.set_email("ygliang2009@gmail.com");
person.add_phonenum("abc");
person.add_phonenum("def");
fstream output("myfile",ios::out|ios::binary);
person.SerializeToString(&buffer); //用这个方法,通常不用SerializeToOstream
output.write(buffer.c_str(),buffer.size());
return 0;
}
编译时要把生成的cpp和源文件一起编译,如下:g++ write_person.cpp person.pb.cpp -o write_person -I your_proto_include_path -L your_proto_lib_path -lprotoc -lprotobuf运行时记得要加上LD_LIBRARY_PATH=your_proto_lib_path
读文件(read_person.cpp):
[cpp] view
plaincopy
#include <iostream>
#include "person.pb.h"
#include <fstream>
#include <string>
using namespace std;
int main(){
Person *person = new Person;
char buffer[BUFSIZ];
fstream input("myfile",ios::in|ios::binary);
input.read(buffer,sizeof(Person));
person->ParseFromString(buffer); //用这个方法,通常不用ParseFromString
cout << person->name() << person->phonenum(0) << endl;
return 0;
}
编译运行方法同上:g++ read_person.cpp person.pb.cpp -o read_person -I your_proto_include_path -L your_proto_lib_path -lprotoc -lprotobuf
[cpp] view
plaincopy
message Person{
required string name = 1;
required int32 age = 2;
optional string email = 3;
enum PhoneType{
MOBILE = 1;
HOME = 2;
WORK = 3;
}
message Phone{
required int32 id = 1;
optional PhoneType type = 2 [default = HOME];
}
repeated string phoneNum = 4; //对应于cpp的vector
}
安装好protoc以后,执行protoc person.proto --cpp_out=. 生成 person.pb.h和person.pb.cpp
写文件(write_person.cpp):
[cpp] view
plaincopy
#include <iostream>
#include "person.pb.h"
#include <fstream>
#include <string>
using namespace std;
int main(){
string buffer;
Person person;
person.set_name("chemical");
person.set_age(29);
person.set_email("ygliang2009@gmail.com");
person.add_phonenum("abc");
person.add_phonenum("def");
fstream output("myfile",ios::out|ios::binary);
person.SerializeToString(&buffer); //用这个方法,通常不用SerializeToOstream
output.write(buffer.c_str(),buffer.size());
return 0;
}
编译时要把生成的cpp和源文件一起编译,如下:g++ write_person.cpp person.pb.cpp -o write_person -I your_proto_include_path -L your_proto_lib_path -lprotoc -lprotobuf运行时记得要加上LD_LIBRARY_PATH=your_proto_lib_path
读文件(read_person.cpp):
[cpp] view
plaincopy
#include <iostream>
#include "person.pb.h"
#include <fstream>
#include <string>
using namespace std;
int main(){
Person *person = new Person;
char buffer[BUFSIZ];
fstream input("myfile",ios::in|ios::binary);
input.read(buffer,sizeof(Person));
person->ParseFromString(buffer); //用这个方法,通常不用ParseFromString
cout << person->name() << person->phonenum(0) << endl;
return 0;
}
编译运行方法同上:g++ read_person.cpp person.pb.cpp -o read_person -I your_proto_include_path -L your_proto_lib_path -lprotoc -lprotobuf
相关文章推荐
- Navicat Premium查询CoreData、SQLit数据库操作
- jquery中attr()方法的使用
- 使用JSONObject来生成JSON和解析JSON
- mysql中如何复制一行数据
- Keepalived+mysql实现高可用
- mongodb 命令零散记录
- 写了一个初中同学——为了有效地完成现有的知识积累
- Spring中的p标签
- 智渔课堂官方免费教程二十一:Java常用类之包装类 自动装箱 自动拆箱
- JavaScript-html标题滚动效果
- 解析数据错误
- 《知识不是力量》读书笔记
- I学霸官方免费教程二十一:Java常用类之包装类 自动装箱 自动拆箱
- Android 调试之 Log和LogCat的详解
- Struct和Class的不同
- H3C S5820x 期望风道方向的注意事项
- Deep Learning——CNN(Convolution Neural Network)
- 修改mysql的默认存储引擎为InnoDB
- 继承QStyledItemDelegate 进度条控件重绘
- sdfs