使用c语言读取、写入和修改INI文件

作者:佚名 上传时间:2023-04-30 运行软件:Visual Studio 2019 软件版本:1.0 版权申诉

该篇示例代码将介绍如何使用c语言读取、写入和修改INI文件。INI文件广泛应用于Windows操作系统中,它被用来保存各种应用程序的配置信息。

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

#define MAX_FILE_SIZE 1024
#define MAX_LINE_SIZE 256

//读取INI文件中指定key的value
int read_ini(const char* file_name, const char* section_name, const char* key_name, char* value)
{
    int is_key_found = 0;
    char section_header[MAX_LINE_SIZE];
    char key[MAX_LINE_SIZE];
    char line[MAX_LINE_SIZE];
    FILE* fp;
    fp = fopen(file_name, "r");
    if(fp == NULL) {
        printf("文件%s不存在!", file_name);
        return -1;
    }
    sprintf(section_header, "[%s]", section_name);
    while (fgets(line, MAX_LINE_SIZE, fp)) {
        if(strstr(line, section_header)) {
            while( fgets(line, MAX_LINE_SIZE, fp) && !strstr(line, "[")) {
                sscanf(line, "%[^=]=%[^\n]", key, value);
                if(strcmp(key, key_name) == 0) {
                    is_key_found = 1;             
                    break;
                }
            }
            if(is_key_found) {
                break;
            }
        }
    }
    fclose(fp);
    if(!is_key_found) {
        printf("key %s not found\n", key_name);
        return -2;
    }
    return 0;
}

//向INI文件中写入或修改key-value
int write_ini(const char* file_name, const char* section_name, const char* key_name, const char* value)
{
    int is_section_found = 0;
    int is_key_found = 0;
    int is_modify = 0;
    char section_header[MAX_LINE_SIZE];
    char key[MAX_LINE_SIZE];
    char line[MAX_LINE_SIZE];
    char buffer[MAX_FILE_SIZE];
    FILE* fp;
    long file_size = 0;
    fp = fopen(file_name, "r+");
    if(fp == NULL) {
        printf("文件%s不存在!", file_name);
        return -1;
    }
    fseek(fp, 0, SEEK_END);
    file_size = ftell(fp);//获取整个文件长度
    fseek(fp, 0, SEEK_SET);
    if(file_size > MAX_FILE_SIZE) {
        printf("文件%s过大!", file_name);
        return -2;
    }
    sprintf(section_header, "[%s]", section_name);
    while (fgets(line, MAX_LINE_SIZE, fp)) {
        strcat(buffer, line);
        if(strstr(line, section_header)) {
            is_section_found = 1;
            while( fgets(line, MAX_LINE_SIZE, fp) && !strstr(line, "[")) {
                strcat(buffer, line);
                sscanf(line, "%[^=]=%[^\n]", key, value); 
                if(strcmp(key, key_name) == 0) {
                    is_key_found = 1;
                    sprintf(line, "%s=%s\n", key, value);
                    strcat(buffer, line);
                    is_modify = 1;
                }
            }
            if(!is_modify) {
                sprintf(line, "%s=%s\n", key_name, value);
                strcat(buffer, line);
            }
            break;
        }
    }
    if(!is_section_found) {
        sprintf(line, "[%s]\n%s=%s\n", section_name, key_name, value);
        strcat(buffer, line);
    }
    if(!is_key_found && is_section_found) {
        sprintf(line, "%s=%s\n", key_name, value);
        strcat(buffer, line);
    }   
    fclose(fp);
    fp = fopen(file_name, "w");
    if(fp == NULL) {
        printf("文件%s不存在!", file_name);
        return -1;
    }
    fwrite(buffer, strlen(buffer), 1, fp);
    fclose(fp);
    return 0;
}

int main()
{
    char value[MAX_LINE_SIZE];
    read_ini("test.ini", "Section1", "key2", value);
    printf("key2的值为:%s\n", value);
    write_ini("test.ini", "Section1", "key4", "4");
    return 0;
}

免责申明:文章和图片全部来源于公开网络,如有侵权,请通知删除 server@dude6.com

用户评论
相关推荐