使用C语言解析INI文件


2006-12-30 22:12
/*
 * File: inifile.h
 * Read INI File
 */
#ifndef _INIFILE_H_
#define _INIFILE_H_

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

/*
 * char* GetInitKey(FileName, Section, Key)
 * Return Key=>Value
 * Ex:
 *
 * config.ini
 * [config]
 * dbhost=localhost
 *
 * Usage:
 * char dbhost[20];
 * strcpy(dbhost,GetInitKey("config.ini", "config", "dbhost"));
 *
 * History:
 * Chen QunShan@20061010 (chqs_at_sina.com)
 * Handle comment line, duplicate section,
 * similar key, partly same key, key string existing
 * in value, trim left space char
 * and the last char is EOF.
 */
char *GetInitKey(char *filename, char *title, char *key)
{
    FILE *fp;
    char tmpLine[1024];
    int rtnval;
    int i = 0;
    int flag = 0;
    char *tmp;
    static char tmpstr[1024];


    if ((fp = fopen(filename, "r")) == NULL) {
    return "have no such file";
    }
    while (!feof(fp)) {
    rtnval = fgetc(fp);
    if (rtnval == EOF) {
        tmpLine[i++] = 0;
    } else {
        tmpLine[i++] = rtnval;
    }
    if (rtnval == '/n' || rtnval == EOF) {
        tmpLine[--i] = 0;
        i = 0;

        if (tmpLine[0] == '/0' || strchr("#;", tmpLine[0]) != NULL)
        continue;    // Skip null line or comment line

        if (tmpLine[0] == '[') {
        tmp = NULL;
        if (flag == 1) {
            // Meet next section
#ifdef PROCESS_DUP_SECTION    // INI file exist duplicate section
            flag = 0;
#else
            break;    // Skip other context
#endif
        }
        } else {
        tmp = strchr(tmpLine, '=');
        }

        if ((tmp != NULL) && (flag == 1)) {
        char *p;

        *tmp = '/0';    // erase '=', spilte Key and Value

        p = strstr(tmpLine, key);
        if (p != NULL) {
            if (p > tmpLine && strchr(" /t", *(p - 1)) == NULL)
            continue;    // exist prefix, mishit key

            p += strlen(key);
            if (*p == '/0' || strchr(" /t", *p) != NULL) {
            tmp++;    // Skip '='
            while (*tmp == ' ' || *tmp == '/t')
                tmp++;    // Skip left lead ' ' or '/t'
            strcpy(tmpstr, tmp);
            fclose(fp);
            return tmpstr;
            }
        }
        } else {
        strcpy(tmpstr, "[");
        strcat(tmpstr, title);
        strcat(tmpstr, "]");
        if (strcmp(tmpstr, tmpLine) == 0) {
            flag = 1;
        }
        }

    }

    if (rtnval == EOF) {
        break;
    }
    }                // end of while
    fclose(fp);
    return "";
}


/*--------------*
 * Test Case: *
 *--------------*
 config.ini


 ;[test]
#p2= 789
;p2= 9
p2 = 456


[test]
p3 = p2
pp2= 45
p2 = "456"


[ip]
portexam=777
[user]
port=666
 *--------------*/


#endif                //_INIFILE_H_
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
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文件读写操作。
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值