纯C语言INI文件解析

这篇博客介绍了如何在Android、Windows和Linux平台上使用纯C语言编写一个简单的INI文件解析库。这个库能够处理INI格式的字符串和文件,支持读取与保存操作。
摘要由CSDN通过智能技术生成

    在一个跨平台( Android 、Windows、Linux )项目中配置文件用 INI 格式,自己写了个解析库,纯C语言的,简单好用。

    可以解析 INI 格式的字符串、解析文件、保存到文件。

    下面是头文件:

#ifndef INI_PARSER_H
#define INI_PARSER_H
#ifdef __cplusplus
extern "C" {
#endif
struct tag_value_list;

struct ini_parser {
    struct tag_value_list * keyvalues;
    int (*parse_file)(struct ini_parser *, const char * file);
    int (*parse_string)(struct ini_parser *, const char *text);
    char * (*value)(struct ini_parser *, const char * key);
    void (*set_value)(struct ini_parser *, const char * key, const char * value);
    void (*remove)(struct ini_parser *, const char *key);
    int (*save_to_file)(struct ini_parser *, const char * file);
};

struct ini_parser * new_ini_parser();
void delete_ini_parser(struct ini_parser *);

#ifdef __cplusplus
}
#endif
#endif // INI_PARSER_H

    下面是源文件:

#include "ini_parser.h"
#include <stdio.h>
#include <string.h>
#include "tag_value.h"

static struct tag_value_pair * parse_line(char *line, int len)
{
    struct tag_value_pair * pair = 0;
    int count = 0;
    char * p = line;
    char * end = 0;
    char * start = line;
    if(!p) return 0;
    while(*p == ' ') p++;


    /*blank line*/
    if(p - line == len ||
            *p == '\r' ||
            *p == '\n' ||
            *p == '\0') return 0;

    /*do not support group*/
    if(*p == '[') return 0;
    /*comments*/
    if(*p == '#') return 0;

    /* extract key *
C语言可以通过使用文件操作函数来读写INI文件INI文件是一种以文本形式存储配置信息的文件,包含一系列的节和键值对。下面是一个简单的示例: 首先,我们需要创建一个INI文件,并写入一些配置信息。 #include <stdio.h> int main() { // 打开INI文件 FILE *file = fopen("config.ini", "w"); if (file == NULL) { printf("无法打开INI文件。\n"); return 1; } // 写入配置信息 fprintf(file, "[System]\n"); fprintf(file, "Version=1.0\n"); fprintf(file, "Author=John Doe\n\n"); fprintf(file, "[Database]\n"); fprintf(file, "Host=localhost\n"); fprintf(file, "Port=3306\n"); fprintf(file, "Username=root\n"); fprintf(file, "Password=123456\n"); // 关闭INI文件 fclose(file); return 0; } 上述代码通过fopen函数创建一个名为config.iniINI文件,并使用fprintf函数写入了一些配置信息,每个节和键值对都以特定的格式写入。写入完成后,可以使用fclose函数关闭文件。 接下来,我们可以通过打开已有的INI文件来读取其中的配置信息。 #include <stdio.h> #include <stdlib.h> int main() { // 打开INI文件 FILE *file = fopen("config.ini", "r"); if (file == NULL) { printf("无法打开INI文件。\n"); return 1; } char line[100]; char section[100]; char key[100]; char value[100]; // 逐行读取INI文件 while (fgets(line, sizeof(line), file) != NULL) { // 解析节 if (line[0] == '[' && line[strlen(line) - 2] == ']') { strncpy(section, line + 1, strlen(line) - 3); section[strlen(line) - 3] = '\0'; } // 解析键和值 else if (sscanf(line, "%[^=]=%[^\n]", key, value) == 2) { printf("节: %s, 键: %s, 值: %s\n", section, key, value); } } // 关闭INI文件 fclose(file); return 0; } 上述代码通过fopen函数打开了名为config.iniINI文件,并使用fgets函数以行为单位读取文件内容。然后,代码解析了每一行的内容,并将解析的结果输出。 这只是一个简单的示例,实际的INI文件读写可能会更加复杂。C语言提供了多种文件操作函数,可以进行更高级的INI文件读写操作。
评论 8
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

foruok

你可以选择打赏

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值