Study notes: C++ external configuration files store parameters without recompiling the code

Table of contents

Use configuration file

Using command line parameters

Use environment variables


Use configuration file

In C++, configuration files can usually be used to store variables and parameters in the program so that these parameters can be modified without recompiling the code. A common practice is to use an external configuration file (such as an INI file) to store these parameters. The program reads the configuration file when it starts and loads the parameters into memory for subsequent use.

Here's a basic way to do it:

1. Define a configuration file class for reading and writing configuration files. This class can support the INI file format.

2. Define the variables that need to be stored as configuration parameters in the program. For example, myconfig.ini writes:

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

3. When the program starts, it first reads the configuration file and loads the parameter values ​​​​in it into the corresponding variables, for example:

ConfigFile cfg("myconfig.ini"); // 读取配置文件
myVar1 = cfg.getIntValue("Section1", "MyVar1", myVar1); // 加载整型参数
myVar2 = cfg.getStringValue("Section1", "MyVar2", myVar2); // 加载字符串参数
myVar3 = cfg.getDoubleValue("Section1", "MyVar3", myVar3); // 加载浮点型参数

4. During the running of the program, the parameter values ​​of the program can be dynamically adjusted by modifying the configuration file.

It should be noted that using configuration files requires certain changes to the code, and the configuration files also require manual maintenance. If you just need to simply adjust a few parameters, you can consider using command line parameters or environment variables.

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

Using command line parameters

Here's how to use command line parameters:

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 parameters and use them as the values ​​of the corresponding variables, 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

How 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 values ​​of the program parameters by setting the corresponding environment variables, 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 using command line parameters or environment variables requires manual settings and is inconvenient to manage. If you need to manage more parameters, it is recommended to use configuration files.

Guess you like

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