以文本模式将消息写入给定文件

Write a message to a given file in text mode

本文关键字:文件 消息 文本 模式      更新时间:2023-10-16

请给我一个例子如何在谷歌协议缓冲区文本模式(不是二进制模式)写消息到给定的文件。

message S1
{
     required string name=1;
     required string family=2;
}
message S2
{
     repeated S1;
}
如果使用此代码在文件中写入消息,如何从文件中读取文本格式?
DebugString();

这很简单,使用Message::DebugString()函数:

 S2 s2;
 std::ofstream out("S2.txt");
 out << s2.DebugString();