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
- iniparser示例程序
- iniparser
- iniparser文档
- INI file and Iniparser
- iniparser的使用
- INI file and Iniparser
- INI file and Iniparser
- 示例程序
- 使用iniparser 处理INI文件
- IniParser+win7解析配置文件INI
- VRML示例程序。
- delphi多线程程序示例
- 树形目录示例程序
- 一个串口程序示例
- windows 服务程序示例
- C程序设计 示例程序
- C#示例 自动更新程序
- corba程序示例
- 瀑布流布局浅析
- 无线通信FPGA设计之2-ASK调制和解调的Verilog实现
- java无难事笔记(十)
- FLV文件格式解析
- Poj 1190 生日蛋糕//回溯,减枝
- iniparser示例程序
- CSS十八条技总结
- 代理服务器-CCProxy
- java设计模式---合成模式2
- OJ6
- 概率论03 条件概率
- C++派生类对象访问基类的protected成员
- Java高新技术--注解Annotation
- Timing diagram of DRAM