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++] = r

最低0.47元/天 解锁文章
3600





