protobuf read/write multiple messages from/to a file
2013-03-05 10:12
453 查看
由于在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
}
其实和自定义一种数据结构没什么区别。
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
}
#include "GFileState.pb.h" #include <fstream> #include <string> #include <iostream> using std::string; using std::fstream; using std::cout; int main(int argc,char* argv[]) { string fPath("message.txt"); string strMsg; char buf[1024] = {0}; fstream f; int size; f.open(fPath.c_str(),std::ios_base::app | std::ios_base::binary); FileState msg,msg2; msg.set_name("D:\\Test1"); msg.set_state(FILE_ACTION_ADD); msg.SerializeToString(&strMsg); //msg.SerializePartialToString(&strMsg); size = strMsg.length(); f.write((char*)&size,sizeof(size)); f.write(strMsg.c_str(),size); f.seekg(std::ios_base::end); msg.set_name("/usr/home/nc/download"); msg.set_state(FILE_ACTION_MODIFY); msg.SerializeToString(&strMsg); size = strMsg.length(); f.write((char*)&size,sizeof(size)); f.write(strMsg.c_str(),size); f.close(); f.open(fPath.c_str(),std::ios_base::in | std::ios_base::binary); f.seekg(std::ios_base::beg); strMsg.clear(); size = 0; while(!f.eof()) { f.read((char*)&size,sizeof(size)); if(size > 0 && size < sizeof(buf)) { f.read(buf,size); msg.ParseFromString(buf); cout<<"name:\t\t"<<msg.name()<<std::endl; cout<<"state:\t\t"<<static_cast<int>(msg.state())<<std::endl; } msg.Clear(); memset(buf,'\0',sizeof(buf)); size = 0; } f.close(); std::cin >>strMsg; return 0; }
相关文章推荐
- How to open/read/write a local file from an applet
- caffe下cnn调试中碰到Check failed: ReadProtoFromBinaryFile(param_file, param) Failed to parse NetParameter
- Configure file like format of ini and read data from ini file or write data to ini file
- Spring Batch Read from DB and Write to File
- read from and write to file
- io - make c++ vector read/write from/to a file - Stack Overflow
- Failed to read Class-Path attribute from manifest of jar file:/XXX问题
- OSG could not find plugin to read objects from file xxx.xxx
- read numbers from file and exchange their positions then write into another file
- 【原创】Unable to read TLD "META-INF/c.tld" from JAR file 解决方法
- How to read a PCap file from Wireshark with C++
- Unable to read TLD "META-INF/c.tld" from JAR file
- 解决Unable to read TLD "META-INF/tlds/struts-bean.tld" from JAR file
- Unable to read TLD "META-INF/tlds/struts-bean.tld" from JAR file "file:/xxxWEB-INF/lib/struts.jar": org.apache.jasper.Ja
- Read a Text File with VBA in Excel, and Write the Text to a Spreadsheet
- How to read data from csv file in c#
- prjGetWriteINIFile - How to Read-Write INI file using VB & API - 读取/写入INI文件 - VB6 + API - SourceCode - HackerJLY
- avassetwrite向同一文件重复写入 暂停,继续录制 how-to-write-to-a-single-file-multiple-times with pause/resume
- JBossAS 6.1.0.Final启动报Unable to read the logging configuration from 'file:logging.properties'
- read\write in C++ from file