iniparser示例程序

来源:互联网 发布:算法统宗分银子问题 编辑:程序博客网 时间:2024/06/05 19:05

下载iniparser,下载地址:http://blog.csdn.net/zahuopuboss/article/details/8817538

将iniparser源代码中src目录下的4个文件dictionary.c、dictionary.h、iniparser.c、iniparser.h和示例程序放到同一个目录下

使用下面的Makefile编译

 

##EXEC = initestCC= gccCFLAGS += -Wall -WerrorCFLAGS += -g -MD -O2CFLAGS += -fPIC -ansi -pedanticSRCS = $(wildcard *.c)OBJS = $(SRCS:%.c=obj/%.o)DEPS = $(SRCS:%.c=obj/%.d)all : $(EXEC)@:$(EXEC) : $(OBJS)$(CC) $(LDFLAGS) $^ -o $@obj/%.o : %.c@mkdir -p obj$(CC) $(CFLAGS) -c $< -o $@clean :rm -rf objsinclude $(DEPS).PHONY : all clean


 

示例程序

 

#include <stdio.h>#include <string.h>#include <stdlib.h>#include <stdarg.h>#include "iniparser.h"#define BUF_SIZ 4096static voidusage(void){printf( "  new section           : exec inifile section\n""  get key's value       : exec inifile section key\n""  new or edit key=value : exec inifile section key value\n""  it will be created if inifile is not existed\n");}static dictionary *ini_parse(char *ini_name){FILE *ini = NULL;dictionary *dict = NULL;/* make sure the ini file is exist */ini = fopen(ini_name, "a");if (ini == NULL) {perror("fopen() a");goto out;}fclose(ini);dict = iniparser_load(ini_name);if (dict == NULL) {printf("cannot parse file: %s\n", ini_name);}out:return dict;}static voidini_set(dictionary *dict, char *section_key, char *value){int ret = 0;ret = iniparser_set(dict, section_key, value);if (ret < 0) {printf("%s set error", section_key);}}static voidini_get(dictionary *dict, char *section_key, char *buf, int len){char *s = NULL;s = iniparser_getstring(dict, section_key, NULL);if (s == NULL) {printf("%s not found\n", section_key);return;}strncpy(buf, s, len);}static voidini_save(dictionary *dict, char *ini_name){FILE *ini = NULL;ini = fopen(ini_name, "w");if (ini == NULL) {perror("fopen() w");printf("cannot save file: %s\n", ini_name);return;}iniparser_dump_ini(dict, ini);fclose(ini);}static voidini_process(char *ini_name, char *section, char *key, char *value, char *buf, int len){int ret = 0;dictionary *dict = NULL;char *section_key = NULL;int size = 0;dict = ini_parse(ini_name);if (dict == NULL) {printf("cannot parse file: %s\n", ini_name);return;}if (key == NULL) {ini_set(dict, section, NULL);goto save;}size = strlen(section) + strlen(key) + 1 + 1;/* ':' + '\0' */section_key = calloc(size, 1);if (section_key == NULL) {perror("calloc()");goto out;}ret = sprintf(section_key, "%s:%s", section, key);if (ret < 0) {printf("snprintf error\n");goto free_buf;}if (value) {ini_set(dict, section, NULL);/* make sure the section is exist */ini_set(dict, section_key, value);} else {ini_get(dict, section_key, buf, len);}free_buf:free(section_key);save:ini_save(dict, ini_name);out:iniparser_freedict(dict);}intmain(int argc, char *argv[]){char *ini_name = NULL;char *section = NULL;char *key = NULL;char *value = NULL;char buf[BUF_SIZ];if (argc < 3) {usage();exit(1);}memset(buf, 0, sizeof(buf));ini_name = argv[1];section = argv[2];if (argc > 3) {key = argv[3];}if (argc > 4) {value = argv[4];}ini_process(ini_name, section, key, value, buf, sizeof(buf) - 1);printf("%s\n", buf);return 0;}


 

运行结果

 

$ ./initest test.ini aa$ ./initest test.ini aa 11 11$ ./initest test.ini aa 22 22$ cat test.ini               [aa]11                             = 1122                             = 22


 

原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 淘宝禁运品怎么办呢 货物退回日本了怎么办 淘宝卖家寄多了衣服怎么办 集运地址选错怎么办 淘宝卖韩国化妆品退货怎么办 去韩国留学手机怎么办 韩国办无线网怎么办 淘宝卖家被骗怎么办 淘宝买软件被骗怎么办 被淘宝店诈骗怎么办? 支付宝被骗2000怎么办 给私人打款后不发货怎么办 毕业证寄丢了怎么办 微商下单返现被骗一千四怎么办 淘宝买东西卡里多扣钱怎么办 付款了卖家不发货怎么办 淘宝客服不解决问题怎么办 淘宝未付款订单怎么办 淘宝被限制购买怎么办 苹果官换机维修过怎么办 iphone x官换机坏了怎么办 小娃不要大人睡怎么办? 深度睡眠太少怎么办 踏板摩托车淹缸怎么办 电喷摩托车淹缸怎么办 踏板摩托不过油怎么办 火花塞被汽油淹怎么办 踏板车淹缸了怎么办 电喷汽车淹缸怎么办 踏板摩托车粘缸怎么办 鬼火打不着火怎么办 踏板摩托车没电怎么办 买摩托车被骗了怎么办 鬼火电瓶坏了怎么办 摩托闷油了怎么办 鬼火发不着火怎么办 淘宝买东西发错货了怎么办 咸鱼售假处罚怎么办 趣头条登录不上怎么办 淘宝集运超租怎么办 淘宝发货多发了怎么办