Comment #6 on issue 469 by [email protected]: how to write/read more than
one piece of message to/from a text file in c++
http://code.google.com/p/protobuf/issues/detail?id=469
As said by https://developers.google.com/protocol-buffers/docs/techniques,
the following code is passed to test.
Generally,it's a way that you yourself encode the string and decode it.
#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;
}
--
You received this message because this project is configured to send all
issue notifications to this address.
You may adjust your notification preferences at:
https://code.google.com/hosting/settings
--
You received this message because you are subscribed to the Google Groups "Protocol
Buffers" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/protobuf?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.