利用protobuf进行读写配置文件

news/2024/7/23 11:07:05

利用protobuf进行读写配置文件

1、编写protobuf 的proto文件

在程序中经常会用配置文件,而利用protobuf可以很方便的进行配置文件的读写。
先编写好protobuf的proto文件

syntax = "proto3";
package msgType;

enum EnumTest
{
	TEST0 = 0x00;
	TEST1 = 0x01;
	TEST2 = 0x02;
	TEST3 = 0x03;
}

message ProtoTestSub
{
	int32 test1 = 1;
	string test2 = 2;
}

message ProtoTest
{
	int32 int32_test = 1;
	string str_test = 2;
	repeated double dou_test = 3;
	repeated ProtoTestSub sub_test = 4;
	EnumTest eunm_test = 5;
	bytes bytes_test = 6;
}

2、生成相对应的源文件

root@root:/root/protobuf# protoc --cpp_out=./ ./test.proto.proto
root@root:/root/protobuf# ls
a.out  include  mybuild  protobuf_main.cpp  test.pb.cc  test.pb.h  test.proto

3、示例程序(读写配置文件)

#include <iostream>
#include <vector>
#include <algorithm>

#include "test.pb.h"
#include <google/protobuf/text_format.h>
#include <google/protobuf/io/zero_copy_stream_impl.h>

#include <unistd.h>
#include <string.h>
#include <errno.h>
#include <string>
#include <fstream>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdlib.h>

int main(void )
{
	//1、序列化 填写基本的配置项
	msgType:: ProtoTest cls_proto_test1;
    cls_proto_test1.set_int32_test(123);
    cls_proto_test1.set_str_test("wanxuexiang");
    cls_proto_test1.add_dou_test(56.023);
    cls_proto_test1.add_dou_test(78.023);
    msgType:: ProtoTestSub * pcls_temp = cls_proto_test1.add_sub_test();
    pcls_temp->set_test1(12);
    pcls_temp->set_test2("zxcvbnm");
    pcls_temp = cls_proto_test1.add_sub_test();
    pcls_temp->set_test1(34);
    pcls_temp->set_test2("asdfghjkl");
    cls_proto_test1.set_eunm_test(msgType::TEST0);
    //protobuf调试打印函数 打印成字符串
    cls_proto_test1.PrintDebugString();
    //2、将配置写入文件之中
    std::string str_proto_test1;
	google::protobuf::TextFormat::PrintToString(cls_proto_test1, &str_proto_test1);
	std::ofstream file_proto_test1;
	file_proto_test1.open("file_proto_test1.cfg", std::ios::out| std:: ios_base::ate);

	if (!file_proto_test1.is_open())
	{
	    fprintf(stderr, "open file_proto_test1.cfg fail\n");
	    return -1;
	}
	file_proto_test1 << str_proto_test1;
	file_proto_test1.flush();
	file_proto_test1.close();
	system("cat file_proto_test1.cfg");
    //2、从配置文件中读取数据
    int fd = open("file_proto_test1.cfg", O_RDONLY);
    if (fd < 0)
    {
        printf("file_proto_test1.cfg:%s \n",strerror(errno));
        return false;
    }
    google::protobuf::io::FileInputStream fileInput(fd);
    fileInput.SetCloseOnDelete(true);
    msgType:: ProtoTest cls_proto_test2;;
    google::protobuf::TextFormat::Parse(&fileInput, &cls_proto_test2);
    //protobuf调试打印函数 打印成字符串
    cls_proto_test2.PrintDebugString();
    msgType:: ProtoTestSub cls_temp ;
    for(int i = 0;i < cls_proto_test2.sub_test_size();i++)
    {
        cls_temp = cls_proto_test2.sub_test(i);
        cls_temp.PrintDebugString();
    }
    while(1)
    {
    	sleep(1);
    }	    
    return 0;
}

4、运行结果

root@ubuntu:/wan/protobuf# ./a.out 
int32_test: 123
str_test: "wanxuexiang"
dou_test: 56.023
dou_test: 78.023
sub_test {
  test1: 12
  test2: "zxcvbnm"
}
sub_test {
  test1: 34
  test2: "asdfghjkl"
}
int32_test: 123
str_test: "wanxuexiang"
dou_test: 56.023
dou_test: 78.023
sub_test {
  test1: 12
  test2: "zxcvbnm"
}
sub_test {
  test1: 34
  test2: "asdfghjkl"
}
int32_test: 123
str_test: "wanxuexiang"
dou_test: 56.023
dou_test: 78.023
sub_test {
  test1: 12
  test2: "zxcvbnm"
}
sub_test {
  test1: 34
  test2: "asdfghjkl"
}
test1: 12
test2: "zxcvbnm"
test1: 34
test2: "asdfghjkl"
^C
root@ubuntu:/wan/protobuf# ls
a.out  file_proto_test1.cfg  include  mybuild  protobuf_main.cpp  test.pb.cc  test.pb.h  test.proto
root@ubuntu:/wan/protobuf# cat file_proto_test1.cfg 
int32_test: 123
str_test: "wanxuexiang"
dou_test: 56.023
dou_test: 78.023
sub_test {
  test1: 12
  test2: "zxcvbnm"
}
sub_test {
  test1: 34
  test2: "asdfghjkl"
}

http://www.niftyadmin.cn/n/1868001.html

相关文章

分析 snprintf sprintf printf 等 类“printf” 格式化输出函数,由于类型不兼容造成的内存混乱

分析 snprintf sprintf printf 等 类“printf” 格式化输出函数&#xff0c;由于类型不兼容造成的内存混乱 1、引言 在c/c中有关字符串的格式输出的方法&#xff0c;一般都采用类“printf”系列函数&#xff0c;文件&#xff1a;fprintf 缓存区&#xff1a;sprintf snprintf …

POP3邮件接收协议介绍

一、POP3简介 POP3&#xff08;Post Office Protocol version3&#xff09;&#xff0c;即“邮局协议版本3”。是TCP/IP协议族中的一员&#xff0c;由RFC1939 定义。本协议主要用于支持使用客户端远程管理在服务器上的电子邮件 二、POP3详解 1、通信过程 1&#xff09;、建…

用#if实现3个分支以上宏定义判断

用#if实现3个分支以上宏定义判断 在实际开发过程中经常会用到宏定义的判断&#xff0c;尤其是在硬件平台和软件平台较多的情况下&#xff0c;宏定义可以很好地在不同平台下条件编译&#xff0c;通常 #ifdef 智能实现单一的判断 下面利用 #if defined 实现3个分支以上的条件编译…

C99相比C89新增内容

文章转自 https://blog.csdn.net/lengye7/article/details/80255833 C99相比C89新增内容 由于c99相对于c89有一些废除内容&#xff0c;所以c99并不兼容c89&#xff0c;以下为c99相对于c89的改动。 1、增加restrict指针 C99中增加了公适用于指针的restrict类型修饰符&#x…

WorkBench3.2安装指南

WorkBench3.2安装指南 0 、引言 本文介绍一种直接在win7或者win10下安装WorkBench的方式而不是利用虚拟机&#xff0c;通过VMware或者virtualbox等软件虚拟出网卡&#xff0c;改变虚拟网卡地址的方式完成workbench秘钥的验证。 1、WorkBench3.2 下载WorkBench3.2 安装包 直…

debian 更换软件源

debian 更换软件源 1、备份系统源文件 /etc/apt$ cp -p sources.list ./sources.list.back2、添加源 下面以中国科技大学为例 sed -i s/deb.debian.org/mirrors.ustc.edu.cn/g /etc/apt/sources.list3、更新源 apt-get update

利用gcc __sync_系列函数实现原子操作

利用gcc __sync_系列函数实现原子操作 1、gcc __sync_系列函数介绍 Gcc 4.1.2版本之后&#xff0c;支持内置原子操作。就是说&#xff0c; 不需要引入第三方库&#xff08;如pthread&#xff09;的锁保护&#xff0c;即可对 1、2、4、8字节的数值或指针类型&#xff0c;进行原…

c++11 atomic 之 atomic_flag 使用

c11 atomic 之 atomic_flag 使用 c11 引入了原子操作部分&#xff0c;为无锁编程提供了极大的便利&#xff0c;本系列博文主要涉及一下几个方面 atomic_flag&#xff1a;c11 <atomic> 头文件中最简单的原子类型: 。atomic_flag 一种简单的原子布尔类型&#xff0c;只支…