#pragma warning( disable : 4996 ) //禁止此头文件内的4099警告
#pragma warning( disable : 4244 ) //禁止此头文件内的4099警告
#pragma warning( disable : 4267 ) //禁止此头文件内的4099警告
#pragma warning( disable : 4099 ) //禁止此头文件内的4099警告
#include <iostream>
#include <fstream>
#include <string>
//#pragma warning(push) //
//相关代码或者头文件
#include "./proto/addressbook.pb.h"
//#pragma warning(pop) //恢复到最初的警告级别
using namespace std;
#ifdef _DEBUG
#pragma comment(lib, "./VC8-2.6.0/Debug/libprotobuf.lib")
#else
#pragma comment(lib, "./VC8-2.6.0/Release/libprotobuf.lib")
#endif
int main(int argc, char* argv[])
{
//校验协议版本
GOOGLE_PROTOBUF_VERIFY_VERSION;
tutorial::Person person;
//将数据写到person.pb文件
person.set_id(123);
person.set_name("Bob");
person.set_email("bob@example.com");
fstream out("person.pb", ios::out | ios::binary | ios::trunc);
person.SerializeToOstream(&out);
out.close();
tutorial::Person person1;
//从person.pb文件读取数据
fstream in("person.pb", ios::in | ios::binary);
if (!person1.ParseFromIstream(&in)) {// 从istream 中读取
cerr << "Failed to parse person.pb." << endl;
exit(1);
}
std::cout << "----------------------------------------------------------------------------" << std::endl;
std::cout << "---- bool IsInitialized() -> " << person1.IsInitialized() << std::endl;// 是否初始化
std::cout << "---- string DebugString() -> " << "\r\n"<< person1.DebugString() << std::endl;// 获取可读的调试输出
std::cout << "---- void Clear() -> " << std::endl; person1.Clear();// 清空
std::cout << "----------------------------------------------------------------------------" << std::endl;
std::cout << "---- bool IsInitialized() -> " << person1.IsInitialized() << std::endl;
cout << "ID:" << person1.id() << endl;
cout << "name:" << person1.name() << endl;
if (person1.has_email()) {// 验证字段是否有效
cout << "has e-mail:" << person1.email() << endl;
}
std::cout << "----------------------------------------------------------------------------" << std::endl;
std::cout << "---- void CopyFrom(const Person&) -> " << std::endl;// 指定信息 覆盖
person1.CopyFrom(person);
std::cout << "---- bool IsInitialized() -> " << person1.IsInitialized() << std::endl;
cout << "ID:" << person1.id() << endl;
cout << "name:" << person1.name() << endl;
if (person1.has_email()) {
cout << "has e-mail:" << person1.email() << endl;
}
{
std::cout << "----------------------------------------------------------------------------" << std::endl;
std::cout << "---- bool SerializeToString(string*) -> " << std::endl;
std::string str_SerizlizeToString;
if(person1.SerializeToString(&str_SerizlizeToString))// 序列化到一个string中,存储的还是二进制的数据,只是用string作为一个容器
{
std::cout << "-------- "<< str_SerizlizeToString << std::endl;
}
tutorial::Person person2;
person2.Clear();
if(person2.ParseFromString(str_SerizlizeToString))// 由string 二进制字符进行解析
{// 解析成功
std::cout <<"---- 解析成功" << std::endl;
std::cout << person2.SerializeToOstream(&std::cout) << std::endl;
std::cout << "DebugString \r\n"<<person2.DebugString() << std::endl;
}else
{
std::cout <<"---- 解析失败" << std::endl;
}
}
getchar();
return 0;
}
【google protobuf】
最新推荐文章于 2021-01-14 20:53:10 发布