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

Write a message to a given file in text mode

请举例说明如何将消息写入 Google 协议缓冲区文本模式(不是二进制模式)中的给定文件。

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();