ini 配置文件使用方法

本博文由 youngpan1101 出品,转载请注明出处。
文章链接: http://blog.csdn.net/youngpan1101/article/details/73773296
作者: 宋洋鹏(youngpan1101)
邮箱: yangpeng_song@163.com


简介

  • 一旦程序需要修改一些参数必须要修改程序代码本身并重新编译,为了让程序出厂后还能根据需要进行必要的配置,所以要用配置文件;
  • 配置文件有很多种,如 INI 配置文件,XML 配置文件,cfg 配置文件,还有就是可以使用系统注册表等;
  • INI 就是英文 “initialization” 的头三个字母的缩写;当然 INI file 的后缀名也不一定是 .ini 也可以是 .cfg,.conf 或者是 .txt。

运行 Demo

  1. 下载 inifile.cpp 和 inifile.h 文件,复制到工程目录下的 3rdParty/inifile 文件夹里 【Download

  2. CMakeLists.txt

    cmake_minimum_required(VERSION 2.8)
    project( Sample )
    
     # produce lib
    SET(IniFileParse_CPP  ${PROJECT_SOURCE_DIR}/3rdParty/inifile/inifile.cpp)
    ADD_LIBRARY(IniFileParse  SHARED  ${IniFileParse_CPP})
    
    include_directories( ${PROJECT_SOURCE_DIR}/3rdParty )
    
    add_executable( main main.cpp )
    
    TARGET_LINK_LIBRARIES( main  IniFileParse )
  3. main.cpp

     #include <iostream>
     #include <sstream>
    
     #include "inifile/inifile.h"
    
    //模板函数:将string类型变量转换为常用的数值类型(此方法具有普遍适用性)  
    template <class Type>  
    Type stringToNum(const std::string& str){  
        istringstream iss(str);  
        Type num;  
        iss >> num;  
        return num;      
    } 
    
    void getPara(const std::string& iniPath){
        std::cout << "load ini file to get parameters" << std::endl;
    
        inifile::IniFile ini;   
    
        int flag = ini.load(iniPath);
    
        int ret = 0;   // ret = 0 : succeed to get the parameter , otherwise -1 means fail
    
        int valTmp = ini.getIntValue("CameraID", "ID", ret);
        if(-1 == ret){
            std::cout << "can not get the CameraID from given ini file" << std::endl;
        }
        std::cout << "ret = " << ret << std::endl;
        std::cout << "valTmp = " << valTmp << std::endl;
    
        std::string strTmp = ini.getStringValue("Camera_Info", "SN", ret);
        if(-1 == ret){
            std::cout << "can not get the SN from given ini file" << std::endl;
        }
        std::cout << "ret = " << ret << std::endl;
        std::cout << "strTmp = " << strTmp << std::endl;
    
        double dValTmp = ini.getDoubleValue("Camera_Info", "image_width", ret);
        if(-1 == ret){
            std::cout << "can not get the image_width from given ini file" << std::endl;
        }
        std::cout << "ret = " << ret << std::endl;
        std::cout << "dValTmp = " << dValTmp << std::endl;
    
        // 2 x 4 Matrix 
        std::stringstream Matrix_1(ini.getStringValue("Camera_Info", "Matrix_row_1", ret));
        double num[4];
        for (int i = 0; i < 4; ++i)
        {
          std::string singleValueStr;
          getline(Matrix_1, singleValueStr, ',');
          num[i] = stringToNum<double>(singleValueStr);
        }
        std::cout << "num[0] = " << num[0] << " num[1] = " << num[1] << " num[2] = " << num[2] << " num[3] = " << num[3] << std::endl;
    
        std::stringstream Matrix_2(ini.getStringValue("Camera_Info", "Matrix_row_2", ret));
        for (int i = 0; i < 4; ++i)
        {
          std::string singleValueStr;
          getline(Matrix_2, singleValueStr, ',');
          num[i] = stringToNum<double>(singleValueStr);
        }
        std::cout << "num[0] = " << num[0] << " num[1] = " << num[1] << " num[2] = " << num[2] << " num[3] = " << num[3] << std::endl;
    }
    
    int main (int argc, char** argv){
        std::cout << "ini file parse..." << std::endl;
    
        std::string iniPath = "/home/hwj/Code/sample/para.ini";
    
        getPara(iniPath);
    
        return 1;
    }
  4. para.ini

    [CameraID]
    ID=3      ; the id of camera used to get video stream
    
    [Camera_Info]
    SN=HW_987654321    ; serial number of camera
    image_width=640
    Matrix_row_1=1,2.34,5.4,5.5  
    Matrix_row_2=11,12.34,15.4,15.5
  5. 编译运行

    $ cd build
    $ cmake ..
    $ make
    $ sudo ./main

Reference

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值