Comment #5 on issue 469 by wuy...@gmail.com: 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

Yes,it is just binary data chunk. When I open the file stored some protobuf messages, it seems like that the SerializeToString method forms a c++ string with '\n' as the last character. And when debugging, the first character of the serielized string is '\n' whose ASCII code is 0x0A. if you try parse it in this way,it will work: head a character '\n' at the buffer that receive the parsed binary data chunk gotten from readline.the c++ example code is like:
        string fPath("message.txt");
        string strMsg;
        char buf[1024] = {0};
        fstream f;

        f.open(fPath.c_str(),std::ios_base::in | std::ios_base::binary);
        f.seekg(std::ios_base::beg);
        while(!f.eof())
        {       
                buf[0] = '\n';
                f.getline(buf+1,sizeof(buf) - sizeof(char));
                string strTmp(buf);
                if(!strTmp.empty())
                {
                        msg.ParseFromString(buf);
                        //msg.ParsePartialFromString(strTmp);
                        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));
        }

At most of the time, it will work, yet there is a problem: when it failed to parse, you can do nothing, reading source code, it's a much lot of work. And I have come across this problem. In other words, the parsing method by heading a character '\n' is unreliable or unstable.

Any one have other Feasible way to read/write messages from/to a file? Thanks.

--
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 protobuf+unsubscr...@googlegroups.com.
To post to this group, send email to protobuf@googlegroups.com.
Visit this group at http://groups.google.com/group/protobuf?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.


Reply via email to