C++ configures the ini file to modify the parameters in the program without recompiling the program every time

Change program parameters without recompiling the program

use ini file

In the ini file, it can be configured in the following format:

[x_pose]
value=0.5

[y_pose]
value=0.5

[z_pose]
value=1

Among them, `[x_pose]`, `[y_pose]`, `[z_pose]` are the configuration item names section, and `value` is the value of the configuration item.
In the cpp file, you can use third-party libraries such as boost or the QSettings class provided by Qt to read the ini file.

The boost library reads the ini file

Taking the boost library as an example, the ini file can be read as follows:

#include <boost/property_tree/ptree.hpp>
#include <boost/property_tree/ini_parser.hpp>
#include <iostream>

int main()
{
    boost::property_tree::ptree pt;
    boost::property_tree::ini_parser::read_ini("example.ini", pt);

    float x_pose = pt.get<float>("x_pose.value");
    float y_pose = pt.get<float>("y_pose.value");
    float z_pose = pt.get<float>("z_pose.value");

    std::cout << "x_pose: " << x_pose << std::endl;
    std::cout << "y_pose: " << y_pose << std::endl;
    std::cout << "z_pose: " << z_pose << std::endl;

    return 0;
}

In the above code, `example.ini` is the name of your ini configuration file, and `x_pose.value`, `y_pose.value`, `z_pose.value` are the corresponding configuration item names and values. Run the above code to read the parameters in the configuration file.

Use Cmake to compile the program.

The content of CMakeLists.txt is as follows:

cmake_minimum_required(VERSION 2.8)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")

project(lab1)

add_executable(lib main.cpp)

Then create the "build" folder, open the terminal in the folder, execute "cmake .." "make", generate the executable file lib, and execute "./lib". Execution output:

x_pose: 0.5
y_pose: 0.5
z_pose: 1

dancing.a库

You can use the INI file class to read and write INI configuration files. The specific steps are as follows:

1. Write the INI configuration file and write the path into the configuration file. For example, add the following to the myconfig.ini file:

[Section1]
FilePath=/usr/myhome/123.txt

2. Introduce the INI file class in the program, and read the path in the configuration file. For example:
 

#include <iostream>
#include "ini/INI.h"

int main() {
    // 读取配置文件
    INIReader reader("myconfig.ini");
    if (reader.ParseError() < 0) {
        std::cout << "Failed to load ini file." << std::endl;
        return 1;
    }

    // 读取配置参数
    std::string filePath = reader.Get("Section1", "FilePath", "");

    // 打开文件并读取内容
    std::ifstream ifs(filePath);
    if (!ifs.is_open()) {
        std::cout << "Failed to open file: " << filePath << std::endl;
        return 1;
    }

    std::string line;
    while (std::getline(ifs, line)) {
        std::cout << line << std::endl;
    }

    return 0;
}

It should be noted that the above code needs to import the `INI.h` header file, and link the `libini.a` library file when linking.

In addition, if the INI configuration file and the program are not in the same directory, you need to specify the absolute or relative path of the configuration file.

But the ini library did not succeed in the experiment, so the boost library is still used:

#include <boost/property_tree/ptree.hpp>
#include <boost/property_tree/ini_parser.hpp>
#include <iostream>
#include <fstream>
using namespace std;

int main()
{
//读取ini配置文件,不需要需要using namespace std;
    boost::property_tree::ptree pt;
    boost::property_tree::ini_parser::read_ini("../example.ini", pt);

    // 读取配置参数
    std::string filePath =pt.get<string>("Section1.FilePath");

    // 打开文件并读取内容
    std::ifstream ifs(filePath);
    if (!ifs.is_open()) {
        std::cout << "Failed to open file: " << filePath << std::endl;
        return 1;
    }

    std::string line;
    while (std::getline(ifs, line)) {
        std::cout << line << std::endl;
    }

    return 0;
}

 Summarize:

ini file is written as follows

[section1]

name1=value1

name2=value2

name3=value3

[section2]

name1=value1

...

Read in the program with the boost library:

    boost::property_tree::ptree pt;
    boost::property_tree::ini_parser::read_ini("example.ini", pt);

    float sec1_name1 = pt.get<float>("section1.name1");
    float sec1_name2= pt.get<float>("section1.name2");
    float sec2_name1 = pt.get<float>("section2.name1");

Using command-line arguments or environment variables is also a common way to dynamically adjust program parameters without recompiling the code.

Using command line arguments

Here's how to use command-line arguments:

1. Define variables that need to be dynamically set in the program, for example:

int myVar1 = 100;
string myVar2 = "hello";
double myVar3 = 1.23;

2. When the program starts, parse the command line arguments and use them as the value of the corresponding variable, for example:
 

int main(int argc, char** argv) {
    // 解析命令行参数
    for (int i = 1; i < argc; i += 2) {
        if (strcmp(argv[i], "-myVar1") == 0) {
            myVar1 = atoi(argv[i+1]);
        } else if (strcmp(argv[i], "-myVar2") == 0) {
            myVar2 = argv[i+1];
        } else if (strcmp(argv[i], "-myVar3") == 0) {
            myVar3 = atof(argv[i+1]);
        }
    }

    // 程序正常执行
    // ...
}

3. When starting the program, set the value of the corresponding variable through the command line parameters, for example:

$ ./my_program.exe -myVar1 200 -myVar2 world -myVar3 3.14

use environment variables

The way to use environment variables is as follows:

1. Define variables that need to be dynamically set in the program, for example:

int myVar1 = atoi(getenv("MY_VAR_1") ?: "100");
string myVar2 = (getenv("MY_VAR_2") ?: "hello");
double myVar3 = atof(getenv("MY_VAR_3") ?: "1.23");

2. Before starting the program, set the value of the program parameter by setting the corresponding environment variable, for example:

$ export MY_VAR_1=200
$ export MY_VAR_2=world
$ export MY_VAR_3=3.14
$ ./my_program.exe

It should be noted that the way of using command line parameters or environment variables needs to be set manually, which is not convenient for management. If you need to manage more parameters, it is recommended to use the configuration file.

Guess you like

Origin blog.csdn.net/weixin_56337147/article/details/130846383