c++涓璸rotobuf鎬庝箞璇诲彇鏂囦欢鍙嶅簭鍒楀寲
鍦–++涓紝浣跨敤Protobuf搴撹繘琛屾枃浠剁殑鍙嶅簭鍒楀寲鎿嶄綔鍙互鎸夌収浠ヤ笅姝ラ杩涜锛?/p>
- 瀵煎叆蹇呰鐨勫ご鏂囦欢锛?/li>
#include <fstream>
#include "your_protobuf_generated_header.pb.h"
- 鎵撳紑寰呭弽搴忓垪鍖栫殑鏂囦欢锛屽苟璇诲彇鏂囦欢涓殑鏁版嵁锛?/li>
std::ifstream input("your_file_name", std::ios::binary);
if (!input) {
std::cerr << "Failed to open input file." << std::endl;
return -1;
}
your_protobuf_generated_message message;
if (!message.ParseFromIstream(&input)) {
std::cerr << "Failed to parse input file." << std::endl;
return -1;
}
- 浣跨敤鍙嶅簭鍒楀寲鍚庣殑鏁版嵁杩涜鍚庣画鎿嶄綔锛屽杈撳嚭鎴栧鐞嗭細
std::cout << "Deserialized message: " << message.DebugString() << std::endl;
鍦ㄤ笂杩颁唬鐮佷腑锛?code>your_protobuf_generated_header.pb.h鏄綘鏍规嵁.proto鏂囦欢鐢熸垚鐨勫ご鏂囦欢锛屽叾涓寘鍚簡Protobuf娑堟伅鐨勫畾涔夊拰鐩稿叧鍑芥暟銆?code>your_protobuf_generated_message鏄綘瀹氫箟鐨勫緟鍙嶅簭鍒楀寲鐨勬秷鎭被鍨嬶紝鍙互鏍规嵁瀹為檯鎯呭喌杩涜鏇挎崲銆?/p>
浠ヤ笂灏辨槸鍦–++涓娇鐢≒rotobuf搴撹鍙栨枃浠跺苟杩涜鍙嶅簭鍒楀寲鐨勭畝鍗曠ず渚嬶紝浣犲彲浠ユ牴鎹叿浣撶殑鎯呭喌杩涜璋冩暣鍜屾墿灞曘€?/p>
相关问答