Linux C语言实现读取ini配置文件

这篇博客分享了如何在Linux环境中使用C语言来读取和解析ini配置文件的实用代码,已验证其功能正常。
摘要由CSDN通过智能技术生成

网上收集的代码,亲测可用,代码如下:

/*
 * config.h - declarations/definitions for reading configuration files
 * 
 * Gang He - dchg2000@mail.com .
 */

#ifndef _HEGANG_CONFIG_H_
#define _HEGANG_CONFIG_H_


/*
 * Set line length in configuration files 
 */
#define LINE_LEN 128


/*
 * Define return error code value
 */
#define ERR_NONE 0        /* read configuration file successfully */
#define ERR_NOFILE 2      /* not find or open configuration file */
#define ERR_READFILE 3    /* error occur in reading configuration file */
#define ERR_FORMAT 4      /* invalid format in configuration file */
#define ERR_NOTHING 5     /* not find section or key name in configuration file */


/*
 * Read the value of key name in string form
 */
int getconfigstr(const char* section,        /* points to section name */
                 const char* keyname,        /* points to key name */
                 char* keyvalue,             /* points to destination buffer */
                 unsigned int len,           /* size of destination buffer */
                 const char* filename);      /* points to configuration filename */


/*
 * Read the value of key name in integer form
 */
int getconfigint(const char* section,         /* points to section name */
                 const char* keyname,         /* points to key name */
                 int* keyvalue,               /* points to destination address */
                 const char* filename);       /* points to configuration filename */



#endif


 

 

/* config.c */

#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>

#include "config.h"

static int isremark(const char* line);
static int isjoint(const char* line);
static char* readline(char* line, int n, FILE *stream);
static int getsectio
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值