c++通过fstream读取config配置文件

背景:项目开发过程,模型文件路径较多,初始化中需要读取多个路径。方便管理统一写在一个json文件中。

config.json
{
  "yolov5_mnn":{
    "model_path": "../model/mmrotate/barcode_sim.mnn"
  }
}
demo.cpp
#include <iostream>
#include <fstream>

#include "rapidjson/document.h"
#include "rapidjson/writer.h"
#include "rapidjson/stringbuffer.h"   


    
std::ifstream configFile("../config.json");
// 将文件内容读入字符串
std::string content((std::istreambuf_iterator<char>(configFile)),
                        std::istreambuf_iterator<char>());

// 关闭文件
configFile.close();

// 解析 JSON
rapidjson::Document jsonConfig;
if (!jsonConfig.Parse(content.c_str()).HasParseError()) {
    if (jsonConfig.HasMember("yolov5_mnn")) {
        const rapidjson::Value& yolov5_mnn = jsonConfig["yolov5_mnn"];
        const rapidjson::Value &model_path = yolov5_mnn["model_path"];
        if (model_path.IsString()) {
            yolov5_mnn_path = model_path.GetString();
            std::cout << yolov5_mnn_path << std::endl;
        }
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值