下载iniparser,下载地址:http://blog.csdn.net/zahuopuboss/article/details/8817538
将iniparser源代码中src目录下的4个文件dictionary.c、dictionary.h、iniparser.c、iniparser.h和示例程序放到同一个目录下
使用下面的Makefile编译
# # EXEC = initest CC = gcc CFLAGS += -Wall -Werror CFLAGS += -g -MD -O2 CFLAGS += -fPIC -ansi -pedantic SRCS = $(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 obj sinclude $(DEPS) .PHONY : all clean
示例程序
#include <stdio.h> #include <string.h> #include <stdlib.h> #include <stdarg.h> #include "iniparser.h" #define BUF_SIZ 4096 static void usage(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 void ini_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 void ini_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 void ini_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 void ini_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); } int main(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 = 11 22 = 22