Linux下用C语言读取配置文件

来源:互联网 发布:imf数据库使用指南 编辑:程序博客网 时间:2024/06/09 09:10

Linux下用ccl库可以直接读取配置文件。下面是ccl库的安装和使用方法简单介绍:

一、安装

cd /data0/software/wget http://files.sbooth.org/ccl-0.1.1.tar.gztar zxvf ccl-0.1.1.tar.gzcd ccl-0.1.1./configuremakemake installcd ..ldconfig

二、使用
建立测试程序目录

mkdir /data0/test

进入目录,并创建一个conf文件mytest.conf

cd /data0/testvi mytest.conf

mytest.conf的文件内容如下:

## Sample configuration fileDesktop-Picture = /usr/images/earth.jpgPosition = Centered"Background Color" = Black

编写测试程序test.c,程序内容如下:

#include <stdio.h>#include <ccl/ccl.h>voidusage(const char *prog_name){  printf("usage: %s config_file\n", prog_name);}intmain(int argc,     char **argv){  struct ccl_t                  config;  const struct ccl_pair_t       *iter;  if(argc == 1) {    usage(argv[0]);    return 0;  }  config.comment_char = '#';  config.sep_char = '=';  config.str_char = '"';  ccl_parse(&config, argv[1]);  while((iter = ccl_iterate(&config)) != 0) {    printf("(%s,%s)\n", iter->key, iter->value);  }  ccl_release(&config);  return 0;}

编译程序:

gcc test.c -o test -lccl

测试配置文件的读取

/data0/test/test mytest.conf

程序输入如下:

(Background Color,Black)(Desktop-Picture,/usr/images/earth.jpg)(Position,Centered)

除非注明,本博客文章均为原创,转载请以链接形式标明本文地址
本文地址: http://blog.cnwyhx.com/?p=210

This entry was posted in Linux, 编程札记 and tagged C, Linux by admin. Bookmark the permalink.
0 0
原创粉丝点击