c程序解析ini配置文件
很多程序都有配置文件,配置文件有很多种类型,以下实现ini配置文件中的参数读取功能。
1、ini配置文件
配置文件名称为test.ini,内容如下:
[bmpinfo]
width=1920
height=1080
bit=3
blue=0
green=0
red=255
[bmpinfo_end]
2、c程序代码
主要函数是get_string_from_ini和GetIniKeyString两个。功能是获取ini配置文件中的字符串。如果是整形的配置文件,可以通过atoi转换。
/*******************************************************
* file:testIni.c
* date:2021-05-17
* version:1.0.0.1
* author:jack8126
* description: read para from ini file
*******************************************************/
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <stdarg.h>
const int MAX_KEY_LENGTH = 1024;
//read string from config file
char *get_string_from_ini(char *title, char *key, char *filename)
{
FILE *fp = NULL;
char szLine[1024] = {0};
static char tmpstr[1024] = {0};
int rtnval = 0;
int i = 0;
int flag = 0;
char *tmp = NULL;
if((fp = fopen(filename, "r")) == NULL)
{
perror("fopen()");
return NULL;
}
while(!feof(fp))
{
rtnval = fgetc(fp);
if(rtnval == EOF)
{
break;
}
else
{
szLine[i++] = rtnval;
}
if(rtnval == '\n')
{
szLine[--i] = '\n';
i = 0;
tmp = strchr(szLine, '=');
if((tmp != NULL) && (flag == 1))
{
if(strstr(szLine, key) != NULL)
{
//comment
if('#' == szLine[0]);
else if('/' == szLine[0] && '/' == szLine[1]);
else
{
memset(tmpstr, 0 ,1024);
//local key position
strcpy(tmpstr, tmp+1);
fclose(fp);
//printf("tmpstr=%s\r\n",tmpstr);
return tmpstr;
}
}
}
else
{
strcpy(tmpstr, "[");
strcat(tmpstr, title);
strcat(tmpstr, "]");
if(strncmp(tmpstr, szLine, strlen(tmpstr)) == 0)
{
//encounter title
flag = 1;
}
}
}
}
fclose(fp);
return "";
}
char *GetIniKeyString(char *title, char *key, char *filename)
{
FILE *fp;
int flag = 0;
char sTitle[32], *wTmp;
static char sLine[1024];
sprintf(sTitle, "[%s]", title);
if (NULL == (fp = fopen(filename, "r")))
{
perror("fopen");
return NULL;
}
while (NULL != fgets(sLine, MAX_KEY_LENGTH, fp))
{
/// 这是注释行 ///
if (0 == strncmp("//", sLine, 2)) continue;
if ('#' == sLine[0]) continue;
wTmp = strchr(sLine, '=');
if ((NULL != wTmp) && (1 == flag))
{
if (0 == strncmp(key, sLine, wTmp - sLine)) /// 长度依文件读取的为准 ///
{
sLine[strlen(sLine) - 1] = '\0';
fclose(fp);
return wTmp + 1;
}
}
else
{
if (0 == strncmp(sTitle, sLine, strlen(sLine) - 1)) /// 长度依文件读取的为准 ///
{
flag = 1; /// 找到标题位置 ///
}
}
}
fclose(fp);
return NULL;
}
int get_int_from_ini(char *title, char *key, char *filename)
{
return atoi(get_string_from_ini(title, key, filename));
}
int GetIntFromIni(char *title, char *key, char *filename)
{
return atoi(GetIniKeyString(title, key, filename));
}
int main(int argc, char **argv)
{
char name[1024];
int width = 0;
int height = 0;
int bit = 0;
int blue = 0;
int green = 0;
int red = 0;
memset(name, 0, sizeof(name));
if(argc < 2)
{
printf("please input like this:\r\n");
printf("./testIni.bin bmptest.ini \r\n");
printf("bmptest.ini --------------- ini cfg file \r\n");
return -1;
}
width = get_int_from_ini("bmpinfo", "width", argv[1]);
height = get_int_from_ini("bmpinfo", "height", argv[1]);
bit = get_int_from_ini("bmpinfo", "bit", argv[1]);
blue = get_int_from_ini("bmpinfo", "blue", argv[1]);
green = get_int_from_ini("bmpinfo", "green", argv[1]);
red = GetIntFromIni("bmpinfo", "red", argv[1]);
//puts(name);
printf("width = %d\n", width);
printf("height = %d\n", height);
printf("bit = %d\n", bit);
printf("blue = %d\n", blue);
printf("green = %d\n", green);
printf("red = %d\n", red);
}
3、编译程序
执行编译命令
gcc testIni.c -o testIni.bin
编译后产生了testIni.bin文件
4、执行程序
执行程序命令
./testIni.bin test.ini
执行后,可以打印出来ini文件中的参数
将这个test.ini修改为test.cfg、test、test.conf等后缀的配置文件,也是可以读取其中的参数的。
参考资料:
https://blog.csdn.net/elikang/article/details/85326597
https://blog.csdn.net/fm0517/article/details/88884771