C读取INI的代码实例

简介: C读取INI的代码实例

工作需要,网上找了一点代码,以此为基础做了修改。写的部分还没做,以后有机会再发布。

从方便来看,最好是实现一个字典,把值放进字典中,访问起来特别方便。

/**
  允许section为空,此时搜索文件的全部。
 */
int gh_conf_read_value(const char* psFile, const char* psSection, const char* psKey, char* psValue)
{
    FILE* pFile = NULL;
    char lineContent[LINE_CONTENT_MAX_LEN];
    BOOL bFoundSection = BOOL_FALSE;
    BOOL bFoundKey     = BOOL_FALSE;
    char cHead         = 0;
    int  nLength       = 0;
    char *pHead        = NULL;
    if (psKey == NULL || psFile == NULL || psValue == NULL)
    {
        return -1;
    }
    pFile = fopen(psFile, "r");
    if (pFile == NULL)
    {
        GH_LOG_INFO("%s: Opent file %s failed.\n", __FILE__, psFile);
        return -1;
    }
    //找到sestion
    if (psSection != NULL)
    {
        while (feof(pFile) == 0)
        {
            memset(lineContent, 0, LINE_CONTENT_MAX_LEN);
            fgets(lineContent, LINE_CONTENT_MAX_LEN, pFile);
            cHead   = lineContent[0];
            //去空格
            if ((cHead == '#') || (cHead == '\0') || (cHead == '\r') || (cHead == '\n'))
            {
                continue;
            }
            //合法的section, 自动认为 lineContent[nLength-1-1] == ']'
            if (cHead == '[')
            {
                //[]之外,还有个\n
                nLength = strlen(lineContent)-3;
                if (!strncmp(lineContent+1, psSection, nLength))
                {
                    bFoundSection = BOOL_TRUE;
                    break;
                }
                continue;
            }
        }
        //没找到,直接返回吧。
        if (!bFoundSection)
        {
            fclose(pFile);
            return -1;
        }
    }
    //找KEY
    while (feof(pFile) == 0)
    {
        memset(lineContent, 0, LINE_CONTENT_MAX_LEN);
        fgets(lineContent, LINE_CONTENT_MAX_LEN, pFile);
        cHead = lineContent[0];
        if ((cHead == '#') || (cHead == '\0') || (cHead == '\r') || (cHead == '\n'))
        {
            continue;
        }
        //碰到新的节,要说明没找到。
        if (psSection != NULL && cHead == '[')
        {
            break;
        }
        //check key
        if (strncmp(lineContent, psKey, strlen(psKey)) == 0)
        {
            bFoundKey = BOOL_TRUE;
            break;
        }
    }
    //先关闭,后面省事。
    fclose(pFile);
    pFile = NULL;
    //没找到,直接返回吧。
    if (!bFoundKey)
    {
        return -1;
    }
    //切分值。从第一个等号开始,后面全部是值。
    pHead = strchr(lineContent, '=');
    if (pHead == NULL)
    {
        return -1;
    }
    //跳过=
    pHead++;
    nLength = strlen(pHead)-1;
    //最后一个是回车
    if (pHead[nLength] == '\n')
    {
        pHead[nLength] = 0;
    } 
    strcpy(psValue, pHead);
    return 0;
}
目录
相关文章
|
4月前
|
Python
python对 ini 文件的读取
python对 ini 文件的读取
|
8月前
|
存储 Java C语言
手把手教你实现类似ini4j的方式创建读取和修改.ini文件(支持section)
手把手教你实现类似ini4j的方式创建读取和修改.ini文件(支持section)
72 0
|
10月前
|
存储 API 数据格式
Qt通过QSttings类读取*.ini配置文件
Qt通过QSttings类读取*.ini配置文件
154 0
C读取INI的代码实例
C读取INI的代码实例
60 0
|
Python
Python读取ini配置文件
db_config.ini [baseconf] host=127.0.0.1 port=3306 user=root password=root db_name=evaluting_sys [concurrent] processor=20 ...
1291 0
|
PHP
PHP面试题:请写一段程序,在服务器创建一个文件fruit.dat,将试题3中得到的数组写入到改文件中,然后写一段程序从文件中读取并还原数组@author zhuwenqiong
PHP面试题:请写一段程序,在服务器创建一个文件fruit.dat,将试题3中得到的数组写入到改文件中,然后写一段程序从文件中读取并还原数组@author zhuwenqiong
105 0
|
API
INI文件的写入与读取
INI文件的写入与读取  [节名]         '[]中的节名对应此API的第一参数 Name=内容      'Nmae对应此API的第二参数 API的第三参数是没有取到匹配内容时返回的字符串; API的第四参数是要返回的字符串; API的第五参数是字符串缓冲的长度,一般255; API的第六参数是INI文件的路径。
1314 0
|
关系型数据库 数据库 Python