C/C++读取配置文件的方式小结
更新时间:2024年04月10日 08:39:53 作者:听雨听风眠
这篇文章主要为大家详细介绍了C/C++中读取配置文件的几种常见方式,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下
1. protobuf
将配置信息读取到string流中
/** * @brief 将配置文件读取到string流中 * * @param file_name :配置文件名称 * @param content: 读取后的string配置信息流 * @return true :成功读取 * @return false:打开文件失败 */ bool GetContent(const std::string &file_name, std::string *content) { // 打开文件流 std::ifstream fin(file_name); if (!fin) { return false; } // 获取流缓冲区 std::stringstream str_stream; str_stream << fin.rdbuf(); *content = str_stream.str(); return true; }
创建proto格式配置文件(dag_config.proto)
message DAGConfig { enum SubnodeType { SUBNODE_IN = 1; SUBNODE_OUT = 2; SUBNODE_NORMAL = 3; }; // Subnode instance. message Subnode { required int32 id = 1; required string name = 2; // node private data.节点私有数据 optional string reserve = 3; optional SubnodeType type = 4 [default = SUBNODE_NORMAL]; }; message SubnodeConfig { repeated Subnode subnodes = 1; }; message Event { required int32 id = 1; optional string name = 2; }; message Edge { required int32 id = 1; required int32 from_node = 2; required int32 to_node = 3; repeated Event events = 4; }; message EdgeConfig { repeated Edge edges = 1; } message SharedData { required int32 id = 1; required string name = 2; }; message SharedDataConfig { repeated SharedData datas = 1; } required SubnodeConfig subnode_config = 1; required EdgeConfig edge_config = 2; required SharedDataConfig data_config = 3; };
将proto文件编译成.cc和.h文件
略
使用protobuf的序列化进行配置文件解析
DAGConfig dag_config; string content; // 获取内容 if (!GetContent(dag_config_path, &content)) { return false; } // 解析字符串:使用protobuf的序列化进行配置文件解析,将解析结果放置在dag_config中 if (!TextFormat::ParseFromString(content, &dag_config)) { return false; }
2. opencv
3. json
4. gflags
在代码中设置待配置变量(alg_fusConfig.hpp)
#ifndef INTERFACE_FUS_CONFIG_H #define INTERFACE_FUS_CONFIG_H #include <gflags/gflags.h> DEFINE_bool(use_dataAcquisition, false,"Whether to use data acquisition");// FLAGS_use_dataAcquisition DEFINE_bool(use_ydutool, false,"Whether to use ydutools");// FLAGS_use_ydutool DEFINE_bool(use_aiCarport, true,"Whether to receive visual parking Spaces");// FLAGS_use_aiCarport DEFINE_bool(use_ultrasonic, true,"Whether to use ultrasonic radar");// FLAGS_use_ultrasonic // 输出打印 DEFINE_bool(use_debugPtintf, false,"Whether to use ultrasonic radar"); // FLAGS_use_debugPtintf DEFINE_bool(ptintf_fusCarportList, false,"Whether to use ultrasonic radar"); // FLAGS_ptintf_fusCarportList DEFINE_bool(ptintf_fusSelectedCarport, false,"Whether to use ultrasonic radar"); // FLAGS_ptintf_fusSelectedCarport // 输入打印 DEFINE_bool(ptintf_location, false,"Whether to use ultrasonic radar"); // FLAGS_ptintf_location DEFINE_bool(ptintf_decision, false,"Whether to use ultrasonic radar"); // FLAGS_ptintf_decision DEFINE_bool(ptintf_carSpace, false,"Whether to use ultrasonic radar"); // FLAGS_ptintf_carSpace DEFINE_bool(ptintf_userCarSpace, false,"Whether to use ultrasonic radar"); // FLAGS_ptintf_userCarSpace DEFINE_bool(ptintf_selectedCarport, false,"Whether to use ultrasonic radar"); // FLAGS_ptintf_selectedCarport DEFINE_bool(ptintf_ultrarRadar, false,"Whether to use ultrasonic radar"); // FLAGS_ptintf_ultrarRadar DEFINE_bool(ptintf_fapa, false,"Whether to use ultrasonic radar"); // FLAGS_ptintf_fapa DEFINE_bool(ptintf_vehicle, false,"Whether to use ultrasonic radar"); // FLAGS_ptintf_vehicle #endif
创建配置文件(config.conf)
## 数据采集 --use_dataAcquisition=false --use_ydutool=false --use_aiCarport=true --use_ultrasonic=true ## 输出打印 --use_debugPtintf=false --ptintf_fusCarportList=false --ptintf_fusSelectedCarport=false ## 输入打印 --ptintf_location=false --ptintf_decision=false --ptintf_carSpace=false --ptintf_userCarSpace=false --ptintf_selectedCarport=false --ptintf_ultrarRadar=false --ptintf_fapa=false --ptintf_vehicle=false
在C代码中加载配置文件
// 配置文件所在路径 std::string config_file = "/ota/SW1.0.06/modules/fusion_carport/config/config.conf"; if( fusCarportLog::CheckPath(config_file) == 1)// 判断配置文件存不存在 { // 生成ParseCommandLineFlags 的参数支持的数据类型 std::string stingArray[2]; stingArray[0] = "fusion_carport";// 一般设置为进程名称,可以是任意字符,ParseCommandLineFlags函数要求一定需要设置 stingArray[1] = "--flagfile="; stingArray[1] = stingArray[1] + config_file; int numStrings = sizeof(stingArray) / sizeof(stingArray[0]); char* charPtrArray[numStrings]; for (std::size_t i = 0; i < numStrings; ++i) { charPtrArray[i] = new char[stingArray[i].length() + 1]; std::strcpy(charPtrArray[i], stingArray[i].c_str()); } auto pArgv = &charPtrArray[0]; gflags::ParseCommandLineFlags(&numStrings, &pArgv, true); for (std::size_t i = 0; i < numStrings; ++i) { delete[] charPtrArray[i]; } }else{ printf( "\n-->%s配置文件不存在\n",config_file.c_str()); } printf( "\n FLAGS_use_dataAcquisition=%d\n \ FLAGS_use_ydutool=%d\n \ FLAGS_use_aiCarport=%d\n \ FLAGS_use_ultrasonic=%d\n \ FLAGS_use_debugPtintf=%d\n \ FLAGS_ptintf_fusCarportList=%d\n \ FLAGS_ptintf_fusSelectedCarport=%d\n \ FLAGS_ptintf_location=%d\n \ FLAGS_ptintf_decision=%d\n \ FLAGS_ptintf_carSpace=%d\n \ FLAGS_ptintf_userCarSpace=%d\n \ FLAGS_ptintf_selectedCarport=%d\n \ FLAGS_ptintf_ultrarRadar=%d\n \ FLAGS_ptintf_fapa=%d\n \ FLAGS_ptintf_vehicle=%d\n \ ",FLAGS_use_dataAcquisition,FLAGS_use_ydutool,FLAGS_use_aiCarport,FLAGS_use_ultrasonic,FLAGS_use_debugPtintf, \ FLAGS_ptintf_fusCarportList,FLAGS_ptintf_fusSelectedCarport,FLAGS_ptintf_location,FLAGS_ptintf_decision, \ FLAGS_ptintf_carSpace, FLAGS_ptintf_userCarSpace,FLAGS_ptintf_selectedCarport,FLAGS_ptintf_ultrarRadar,FLAGS_ptintf_fapa, \ FLAGS_ptintf_vehicle);
以上就是C/C++读取配置文件的方式小结的详细内容,更多关于C++读取配置文件的资料请关注脚本之家其它相关文章!
最新评论