protobuf read/write multiple messages from/to a file

简介:

由于在protobuf论坛上发过相关问题,但,根据https://developers.google.com/protocol-buffers/docs/techniques提供的相关解决办法,自己测试下想再反馈给论坛,下面是测试过的,当想放到论坛时,好像那个问题已经关闭了。

其实和自定义一种数据结构没什么区别。

proto file中的定义是:

enum FileAction {
   FILE_ACTION_ADD = 3; 
   FILE_ACTION_DEL = 2; 
   FILE_ACTION_MODIFY = 1; 
   FILE_ACTION_RENAME = 0; 
}

message FileState {
   required string name = 1;     // file name
   required FileAction state = 2;   // file state
}

 
 
  1. #include "GFileState.pb.h" 
  2. #include <fstream> 
  3. #include <string> 
  4. #include <iostream> 
  5. using std::string; 
  6. using std::fstream; 
  7. using std::cout; 
  8. int main(int argc,char* argv[]) 
  9.     string      fPath("message.txt"); 
  10.     string      strMsg; 
  11.     char        buf[1024] = {0}; 
  12.     fstream     f; 
  13.     int         size; 
  14.     f.open(fPath.c_str(),std::ios_base::app | std::ios_base::binary); 
  15.     FileState msg,msg2; 
  16.     msg.set_name("D:\\Test1"); 
  17.     msg.set_state(FILE_ACTION_ADD); 
  18.     msg.SerializeToString(&strMsg); 
  19.     //msg.SerializePartialToString(&strMsg); 
  20.     size        = strMsg.length(); 
  21.     f.write((char*)&size,sizeof(size)); 
  22.     f.write(strMsg.c_str(),size); 
  23.     f.seekg(std::ios_base::end); 
  24.  
  25.     msg.set_name("/usr/home/nc/download"); 
  26.     msg.set_state(FILE_ACTION_MODIFY); 
  27.     msg.SerializeToString(&strMsg); 
  28.      
  29.     size        = strMsg.length(); 
  30.     f.write((char*)&size,sizeof(size)); 
  31.     f.write(strMsg.c_str(),size); 
  32.     f.close(); 
  33.  
  34.     f.open(fPath.c_str(),std::ios_base::in | std::ios_base::binary); 
  35.     f.seekg(std::ios_base::beg); 
  36.     strMsg.clear(); 
  37.     size = 0; 
  38.     while(!f.eof()) 
  39.     {    
  40.         f.read((char*)&size,sizeof(size)); 
  41.         if(size > 0 && size < sizeof(buf)) 
  42.         { 
  43.             f.read(buf,size); 
  44.             msg.ParseFromString(buf); 
  45.             cout<<"name:\t\t"<<msg.name()<<std::endl; 
  46.             cout<<"state:\t\t"<<static_cast<int>(msg.state())<<std::endl; 
  47.         } 
  48.         msg.Clear(); 
  49.         memset(buf,'\0',sizeof(buf)); 
  50.         size = 0; 
  51.     } 
  52.     f.close(); 
  53.     std::cin >>strMsg; 
  54.     return 0; 

 










本文转自 hakuyo 51CTO博客,原文链接:http://blog.51cto.com/hakuyo/1147365,如需转载请自行联系原作者

目录
相关文章
|
10月前
Cannot read properties of undefined (reading ‘resetFields‘)“
Cannot read properties of undefined (reading ‘resetFields‘)“
153 0
|
11月前
UE Operation File [ Read / Write ] DTOperateFile Plug-in Description
UE Operation File [ Read / Write ] DTOperateFile Plug-in Description
48 0
|
1月前
|
JavaScript
Cannot read properties of undefined (reading ‘install‘) TypeError: Cannot read properties of……
Cannot read properties of undefined (reading ‘install‘) TypeError: Cannot read properties of……
|
7月前
|
JavaScript
TypeError: Cannot read properties of null (reading &#39;level&#39;)
# 一、分析问题 1、一个下拉框组件的更新由另一个下拉框组件控制被动更新列表,子级下拉框的值是由父级下拉框的值调用接口获取,每次父级下拉框值的改变都会改变子级下拉框的数据源也就是会改变子级下拉框的options,切换后之前的父级节点找不到就会报了这个错,父级节点不改变(即不切换)的话不会报错 # 二、解决方案 ## 1、vue页面的html层 ```html &lt;div&gt; &lt;el-row :gutter=&quot;15&quot;&gt; &lt;el-col :span=&quot;4&quot;&gt; &lt;div&quot;&gt;父级下拉框:&lt;/div&gt; &lt;el-select clearable v-model=&quot;parentId&quot; @c
95 0
|
11月前
|
Windows
UE INI File Operation [ Read / Write ] Plug-in description
UE INI File Operation [ Read / Write ] Plug-in description
44 0
|
11月前
UE Operation File [ Read / Write ] DTOperateFile 插件说明
UE Operation File [ Read / Write ] DTOperateFile 插件说明
45 0
|
开发工具 Android开发
unable to write jarlist cache file
unable to write jarlist cache file
78 0
Can‘t read file : End of file found 文件:txn_current、current svn无法正常读取文件
Can‘t read file : End of file found 文件:txn_current、current svn无法正常读取文件
Can‘t read file : End of file found 文件:txn_current、current svn无法正常读取文件
|
SQL 测试技术
The process could not read file xxx due to OS error 53
在不同地域的两个SQL Server服务器上配置了复制(Replication)用于同步数据(生产环境配置有Replication,测试环境也配有Replication),两地通过专线连接起来,这些复制(Replication)已经稳定运行了一两年了, 但是前阵子,测试环境的SQL Se...
1072 0
|
关系型数据库
### avoid read-on-write
### avoid read-on-write 什么是 "read-on-write" problem? 在我们使用最常见的buffer write 中 "read-on-write" 问题指的是当我需要进行小于4k 大小buffer write 的时候, 需要先将数据所在的page 从disk 中读取出放入到page cache, 在page cache 中修改好, 然后再将
1447 0