c程序解析ini配置文件

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
评论 3
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值