libconfig c语言实例

在工作项目中,使用到了配置文件导入导出等功能(否则程序就不是可配置的),所以就需要合理选择配置文件得读写操作,libconfig就是这么样一个东西。

Libconfig是一个结构化的配置文件库,它可以定义一些配置文件,例如test.cfg . 它比xml可读性更好,而且更简洁。libconfig主要是通过路径读取对应的 .cfg文件来获取其中的内容,不必要去解析字符串等内容,直接通过函数获取数据,非常方便。

官方的例子虽然简单,但是有点长,通过下面这个简单的例子,向大家接好一下其中几个主要的函数的用法
首先定义测试的配置文件 test.cfg, 其中需要用到的配置数据如下:

		timeFormatComb =
		{
			type = "combo";
			label = "时间格式";
			x = 355;
			y = 170;
			w = 120;
			h = 32;
			member = ["12小时","24小时"];
			comboH = 200;
		};

以下是代码部分:

#include "stdio.h"
#include "libconfig.h"

#define STR_LEN    (128)
#define GUI_MAIN_CFG_PATH "/mnt/hgfs/share/projects/test.cfg"

int main(int argc, const char *argv[])
{
	int ret = 0;
	int result = 0;
	int i= 0;
	int cfgx = 0;
	int cfgy = 0;
	int cfgw = 0;
	int cfgh = 0;
	int comboH = 0;
	int memberCount = 0;
    const char *pLabel = NULL;
	config_t *cfgHandle; /* 配置文件操作句柄 */
    config_setting_t *pCfgSetting = NULL;
    config_setting_t *pMemberDetail = NULL;
	char path[128] = {0};

	pLabel = (char *)malloc(STR_LEN);
	if(pLabel == NULL)
	{
		printf("malloc() error! pLabel = %p\n", pLabel);
		goto EXIT;
	}
    /* 读取配置文件,获取文件句柄 */
	result = config_read_file(cfgHandle, GUI_MAIN_CFG_PATH);
	if(ret != 0)
	{
		printf("config_read_file() error! result = %d\n", result);
	}

	snprintf(path, sizeof(path), "%s", "winSysSymTimeCfgSet.timeCfgPanel.timeFormatComb");

	/* 通过文件句柄查找数据 */
	pCfgSetting = config_lookup(cfgHandle, path);
	if(pCfgSetting == NULL)
	{
		printf("config_lookup() error! path = %s\n", path);
		goto EXIT;
	}
	printf("pCfgSetting = %p", pCfgSetting);

    /* 查找字符串 */
	result = config_setting_lookup_string(pCfgSetting, "label", &pLabel);
	if(result != CONFIG_TRUE)
	{
		guiLogErr("config_setting_lookup_string() error.\n");
		goto EXIT;
	}
	printf("pLabel = %s", pLabel);

    /* 查找int数值 */
	result = config_setting_lookup_int(pCfgSetting, "x", &cfgx);
	if(result != CONFIG_TRUE)
	{
		guiLogErr("config_setting_lookup_int() error!\n");
		goto EXIT;
	}
	
	result = config_setting_lookup_int(pCfgSetting, "y", &cfgy);
	if(result != CONFIG_TRUE)
	{
		guiLogErr("config_setting_lookup_int() error!\n");
		goto EXIT;
	}
	
	result = config_setting_lookup_int(pCfgSetting, "w", &cfgw);
	if(result != CONFIG_TRUE)
	{
		guiLogErr("config_setting_lookup_int() error!\n");
		goto EXIT;
	}
	
	result = config_setting_lookup_int(pCfgSetting, "h", &cfgh);
	if(result != CONFIG_TRUE)
	{
		guiLogErr("config_setting_lookup_int() error!\n");
		goto EXIT;
	}
	printf("x = %d; y = %d; w = %d; h = %d\n", cfgx, cfgy, cfgw, cfgh);

	result = config_setting_lookup_int(pCfgSetting, "comboH", &comboH);
	if(result != CONFIG_TRUE)
	{
		guiLogErr("config_setting_lookup_int() error!\n");
		goto EXIT;
	}
	printf("x = %d; y = %d; w = %d; h = %d\n", cfgx, cfgy, cfgw, cfgh);

    /* 查找数组 */
	pMemberDetail = config_setting_get_member(pCfgSetting, "member");
	if(pMemberDetail == NULL)
	{
		guiLogErr("config_setting_get_member() error!\n");
		goto EXIT;
	}
	
    /* 获取数组长度 */
    memberCount = config_setting_length(pMemberDetail);
	if(memberCount <= 0)
	{
		guiLogErr("config_setting_length() error!\n");
		goto EXIT;
	}
	printf("memberCount = %d\n", memberCount);

	for(i = 0; i < memberCount; i++)
	{
		pLabel = config_setting_get_string_elem(pMemberDetail, i);
	    if(pLabel == NULL) 
	    {
	        guiLogErr("config_setting_get_string_elem = %s\n", pLabel);
	        goto EXIT;
	    }
		printf("pLabel = %s", pLabel);
	}
	config_destroy(&cfgHandle);

EXIT:

	return ret;
}

因为使用到了libconfig库,所以在编译的时候要链接库文件
gcc test.c -o test -lconfig

运行结果如下:
在这里插入图片描述

这个简单的例子,通过一些列的库函数将配置文件中的一些列值取出。因为时间有限,没有花费太多的时间去搞,有兴趣的朋友可以自己试一试。

  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
libconfig是一个C/C++库,用于处理配置文件。使用libconfig可以轻松地读取和写入各种格式的配置文件,包括INI、XML和JSON格式。 以下是libconfig的基本用法: 1. 安装libconfig 可以使用包管理器在Linux上安装libconfig。在Ubuntu上,可以使用以下命令进行安装: ``` sudo apt-get install libconfig-dev ``` 在Windows上,可以从libconfig的官方网站下载预编译的二进制文件,并将其添加到项目中。 2. 创建配置文件 可以使用任何文本编辑器创建配置文件。例如,以下是一个简单的INI格式示例: ``` ; Sample configuration file [database] host = "localhost" port = 3306 username = "root" password = "password123" database = "mydb" ``` 3. 读取配置文件 要读取配置文件,需要创建一个libconfig对象,并使用它来打开配置文件并读取值。以下是一个示例代码: ```c++ #include <iostream> #include <libconfig.h++> using namespace std; using namespace libconfig; int main() { Config cfg; try { cfg.readFile("config.ini"); string host = cfg.lookup("database.host"); int port = cfg.lookup("database.port"); string username = cfg.lookup("database.username"); string password = cfg.lookup("database.password"); string database = cfg.lookup("database.database"); cout << "Host: " << host << endl; cout << "Port: " << port << endl; cout << "Username: " << username << endl; cout << "Password: " << password << endl; cout << "Database: " << database << endl; } catch(const FileIOException &fioex) { cerr << "I/O error while reading file." << endl; return(EXIT_FAILURE); } catch(const ParseException &pex) { cerr << "Parse error at " << pex.getFile() << ":" << pex.getLine() << " - " << pex.getError() << endl; return(EXIT_FAILURE); } return(EXIT_SUCCESS); } ``` 在代码中,我们首先创建了一个Config对象。然后,我们使用readFile()方法打开配置文件。接下来,我们使用lookup()方法获取配置文件中的值,并将其存储在变量中。最后,我们使用cout打印出这些值。 4. 写入配置文件 要写入配置文件,需要创建一个Config对象,并使用它来设置配置文件中的值。以下是一个示例代码: ```c++ #include <iostream> #include <libconfig.h++> using namespace std; using namespace libconfig; int main() { Config cfg; try { cfg.readFile("config.ini"); cfg.lookup("database.host") = "newhost.com"; cfg.lookup("database.port") = 1234; cfg.lookup("database.username") = "newuser"; cfg.lookup("database.password") = "newpassword"; cfg.lookup("database.database") = "newdb"; cfg.writeFile("config.ini"); } catch(const FileIOException &fioex) { cerr << "I/O error while reading/writing file." << endl; return(EXIT_FAILURE); } return(EXIT_SUCCESS); } ``` 在代码中,我们首先创建了一个Config对象。然后,我们使用readFile()方法打开配置文件。接下来,我们使用lookup()方法设置配置文件中的值。最后,我们使用writeFile()方法将更改写入配置文件。 这就是libconfig的基本用法。可以查阅libconfig的官方文档,了解更多高级用法。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

我若成精

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值