.proto文件示例
message Configure { required string host = 1; required uint32 port = 2; }
写配置文件
Configure config;
config.set_host("127.0.0.1");
config.set_port(8080);
string contect; google::protobuf::TextFormat::PrintToString(config, & contect);
ofstream fout; fout.open("config.cfg", ios::out| ios_base::ate); if (!fout.is_open()) { fprintf(stderr, "open config.cfg fail "); return -1; } fout << contect <<endl; fout.flush(); fout.close();
读配置文件
int fd = open("config.cfg", O_RDONLY); if (fd < 0) { printf("open config.cfg failure:%s ",strerror(errno)); return false; } google::protobuf::io::FileInputStream fileInput(fd); fileInput.SetCloseOnDelete(true); google::protobuf::TextFormat::Parse(&fileInput, &config);