LinuxC开发配置⽂件读写库Libconfig
⼀:什么是Libconfig?
程序开发过程中往往很多参数需要放在配置⽂件中,因为这样可以避免更改代码重新编译的问题。通常我们可以通过⾃⼰读init⽂件来实现,不过已经有很多⼈做了这⽅⾯的东西,可以借⽤,Libconfig就是其中之⼀。
Libconfig是⼀个⽤于处理结构化配置⽂件的简单库。 此⽂件格式⽐XML更紧凑,更易读,更适合内存受限的设备。 与XML不同,它是类型感知的,因此不必在应⽤程序代码中进⾏字符串解析。该库⽀持C和C ++语⾔,适⽤于兼容POSIX的UNIX和类UNIX系统(GNU / Linux,Mac OS X,Solaris,FreeBSD),Android和Windows(2000,XP及更⾼版本)。
⼆:怎么使⽤?
1. 下载
2. 解压后执⾏
./configure
make
make install
3. 使⽤可以参考examples
4. 编译时加上 -lconfig++ 或者-lconfig 选项
三:会遇到的问题及注意事项
错误:
Error while loading shared library: libconfig++.so.9
解决⽅式
执⾏:sudo ldconfig -v
(往/lib和/usr/lib⾥⾯加东西,不⽤修改/etc/f,但要调⼀下ldconfig,否则可能这个library会不到)
四、参考⽂献
程序代码包中examples⽬录下的相关⽰例
五、简单测试
1. 配置⽂件⽤例example.cfg,内容如下:
// An example configuration file that stores information about a store.
// Basic store information:
name = "Books, Movies & More";
// Store inventory:
inventory =
{
books = ( { title = "Treasure Island";
author = "Robert Louis Stevenson";
price = 29.99;
qty = 5; },
{ title = "Snow Crash";
author = "Neal Stephenson";
price = 9.99;
qty = 8; }
);
movies = ( { title = "Brazil";
media = "DVD";
price = 19.99;
哈萨克族服饰qty = 11; },
{ title = "The City of Lost Children";
media = "DVD";
price = 18.99;
qty = 5; },
{ title = "Memento";
media = "Blu-Ray";
price = 24.99;
qty = 20;
},
{ title = "Howard the Duck"; }
);
};
// Store hours:
hours =
{
mon = { open = 9; close = 18; };
tue = { open = 9; close = 18; };
wed = { open = 9; close = 18; };
thu = { open = 9; close = 18; };
fri = { open = 9; close = 20; };
sat = { open = 9; close = 20; };
sun = { open = 11; close = 16; };
};
2. 源⽂件example1.c,内容如下:
#include <stdio.h>
#include <stdlib.h>
#include <libconfig.h>
/* This example reads the configuration file 'example.cfg' and displays * some of its contents.
*/
int main(int argc, char **argv)
{
config_t cfg;
config_setting_t *setting;
const char *str;
config_init(&cfg);
/* Read the file. If there is an error, report it and exit. */
if(! config_read_file(&cfg, "example.cfg"))
{
fprintf(stderr, "%s:%d - %s\n", config_error_file(&cfg),
一般车险买哪几种config_error_line(&cfg), config_error_text(&cfg));
config_destroy(&cfg);
情人节朋友圈文案简短return(EXIT_FAILURE);
}
/* Get the store name. 读取配置⽂件中的“Basic store information:”信息 */
if(config_lookup_string(&cfg, "name", &str))
printf("Store name: %s\n\n", str);
else
fprintf(stderr, "No 'name' setting in configuration file.\n");
if(config_lookup_string(&cfg, "hours", &str))
printf("Store hours: %s\n\n", str);
else
fprintf(stderr, "No 'hours' setting in configuration file.\n");
/* Output a list of all books in the inventory. */
setting = config_lookup(&cfg, "inventory.books");
if(setting != NULL)
{
int count = config_setting_length(setting);
int i;
printf("%-30s %-30s %-6s %s\n", "TITLE", "AUTHOR", "PRICE", "QTY");
for(i = 0; i < count; ++i)
{
config_setting_t *book = config_setting_get_elem(setting, i);
/* Only output the record if all of the expected fields are present. */
const char *title, *author;
double price;
int qty;
if(!(config_setting_lookup_string(book, "title", &title)
&& config_setting_lookup_string(book, "author", &author)
&& config_setting_lookup_float(book, "price", &price)
&& config_setting_lookup_int(book, "qty", &qty)))
continue;
printf("%-30s %-30s $%6.2f %3d\n", title, author, price, qty);
}
putchar('\n');
}
/* Output a list of all movies in the inventory. */
setting = config_lookup(&cfg, "vies");
if(setting != NULL)
qq音乐不能播放{
unsigned int count = config_setting_length(setting);
unsigned int i;
printf("%-30s %-10s %-6s %s\n", "TITLE", "MEDIA", "PRICE", "QTY"); for(i = 0; i < count; ++i)
{
config_setting_t *movie = config_setting_get_elem(setting, i);
/* Only output the record if all of the expected fields are present. */
const char *title, *media;
double price;
int qty;
if(!(config_setting_lookup_string(movie, "title", &title)
好听的英文歌曲&& config_setting_lookup_string(movie, "media", &media)
&& config_setting_lookup_float(movie, "price", &price)
&& config_setting_lookup_int(movie, "qty", &qty)))
continue;
printf("%-30s %-10s $%6.2f %3d\n", title, media, price, qty);
}
putchar('\n');
}
config_destroy(&cfg);
return(EXIT_SUCCESS);
}
/* eof */
3. 编译
[root@localhost c]# gcc -lconfig example1.c
[root@localhost c]#
4. 运⾏
[root@localhost c]# ./a.out
Store name: Books, Movies & More
No 'hours' setting in configuration file.
TITLE AUTHOR PRICE QTY Treasure Island Robert Louis Stevenson $ 29.99 5 Snow Crash Neal Stephenson $ 9.99 8
TITLE MEDIA PRICE QTY
Brazil DVD $ 19.99 11
The City of Lost Children DVD $ 18.99 5
典故Memento Blu-Ray $ 24.99 20
[root@localhost c]#
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论