【C语言】libiniparser库使用例子,

2019-07-13 05:21发布

libiniparser是C语言实现的ini文件解析库
使用实例如下 #include #include #include #include #include "iniparser.h" void create_example_ini_file(void); int parse_ini_file(char * ini_name); int main(int argc, char * argv[]) { int status ; if (argc<2) { create_example_ini_file(); status = parse_ini_file("example.ini"); } else { status = parse_ini_file(argv[1]); } return status ; } void create_example_ini_file(void) { FILE * ini ; ini = fopen("example.ini", "w"); fprintf(ini, "# " "# This is an example of ini file " "# " " " "[Pizza] " " " "Ham = yes ; " "Mushrooms = TRUE ; " "Capres = 0 ; " "Cheese = Non ; " " " " " "[Wine] " " " "Grape = Cabernet Sauvignon ; " "Year = 1989 ; " "Country = Spain ; " "Alcohol = 12.5 ; " " "); fclose(ini); } int parse_ini_file(char * ini_name) { dictionary * ini ; /* Some temporary variables to hold query results */ int b ; int i ; double d ; char * s ; ini = iniparser_load(ini_name); if (ini==NULL) { fprintf(stderr, "cannot parse file: %s ", ini_name); return -1 ; } iniparser_dump(ini, stderr); /* Get pizza attributes */ printf("Pizza: "); b = iniparser_getboolean(ini, "pizza:ham", -1); printf("Ham: [%d] ", b); b = iniparser_getboolean(ini, "pizza:mushrooms", -1); printf("Mushrooms: [%d] ", b); b = iniparser_getboolean(ini, "pizza:capres", -1); printf("Capres: [%d] ", b); b = iniparser_getboolean(ini, "pizza:cheese", -1); printf("Cheese: [%d] ", b); /* Get wine attributes */ printf("Wine: "); s = iniparser_getstring(ini, "wine:grape", NULL); printf("Grape: [%s] ", s ? s : "UNDEF"); i = iniparser_getint(ini, "wine:year", -1); printf("Year: [%d] ", i); s = iniparser_getstring(ini, "wine:country", NULL); printf("Country: [%s] ", s ? s : "UNDEF"); d = iniparser_getdouble(ini, "wine:alcohol", -1.0); printf("Alcohol: [%g] ", d); iniparser_freedict(ini); return 0 ; }