【C/C++】C语言读取简单配置文件

用C写了个加载配置文件的demo简单程序, 按行支持 key=val 的格式以及行注释/文本注释等。

以下贴下代码, 纯当笔记:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define MAX_BUF_LEN 1024
#define MAX_KEY_LEN 64
#define MAX_VAL_LEN 256

int Trim(char s[])
{
    int n;
    for(n = strlen(s) - 1; n >= 0; n--)
    {
        if(s[n]!=' ' && s[n]!='\t' && s[n]!='\n')
            break;
        s[n+1] = '\0';
    }
    return n;
}

int loadConfigDemo(const char* config_path)
{
    FILE * file = fopen(config_path, "r");
    if (file == NULL)
    {
        printf("[Error]open %s failed.\n", config_path);
        return -1;
    }


    char buf[MAX_BUF_LEN];
    int text_comment = 0;
    while(fgets(buf, MAX_BUF_LEN, file) != NULL)
    {
        Trim(buf);
        // to skip text comment with flags /* ... */
        if (buf[0] != '#' && (buf[0] != '/' || buf[1] != '/'))
        {
            if (strstr(buf, "/*") != NULL)
            {
                text_comment = 1;
                continue;
            }
            else if (strstr(buf, "*/") != NULL)
            {
                text_comment = 0;
                continue;
            }
        }
        if (text_comment == 1)
        {
            continue;
        }


        int buf_len = strlen(buf);
        // ignore and skip the line with first chracter '#', '=' or '/'
        if (buf_len <= 1 || buf[0] == '#' || buf[0] == '=' || buf[0] == '/')
        {
            continue;
        }
        buf[buf_len-1] = '\0';


        char _paramk[MAX_KEY_LEN] = {0}, _paramv[MAX_VAL_LEN] = {0};
        int _kv=0, _klen=0, _vlen=0;
        int i = 0;
        for (i=0; i<buf_len; ++i)
        {
            if (buf[i] == ' ')
                continue;
            // scan param key name
            if (_kv == 0 && buf[i] != '=')
            {
                if (_klen >= MAX_KEY_LEN)
                    break;
                _paramk[_klen++] = buf[i];
                continue;
            }
            else if (buf[i] == '=')
            {
                _kv = 1;
                continue;
            }
            // scan param key value
            if (_vlen >= MAX_VAL_LEN || buf[i] == '#')
                break;
            _paramv[_vlen++] = buf[i];
        }
        if (strcmp(_paramk, "")==0 || strcmp(_paramv, "")==0)
            continue;
        printf("%s=%s\n", _paramk, _paramv);
    }
    return 0;
}

int main()
{
    loadConfigDemo("./test.conf");
    return 0;
}

其中用于测试的配置文件如下:

# basic
   version=1

// for testing line comment
# fan settings
fan_temp_level=2
fan_pre_heat=1

/* test text comment = test text comment
test text comment = test text comment
// just for test
*/
# /* just for test


# wash
wash_water_temp_level =   2
wash_water_pressure_level=2

# drying
drying_pressure_level=2
drying_temp_level=2

# light
light_threshold=500
light_rgb=4452563       # use 3bytes to store this value: R G B

# voice
volume=45    # range from 0 to 100
演示效果:

xiaomo:/tmp# ./test
version=1
fan_temp_level=2
fan_pre_heat=1
wash_water_temp_level=2
wash_water_pressure_level=2
drying_pressure_level=2
drying_temp_level=2
light_threshold=500
light_rgb=4452563
volume=45

  • 4
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值