linux C获取ini配置文件

#include 
#include 
#include 
#include ;
int GetCfgItem(char* sFileName, char* sKey, char* sField, char* sValues)
{
    FILE* fp = fopen(sFileName, "rb");
    char szBuff[1024];
    char szTemp[1024];

    memset(szTemp, 0x00, sizeof(szTemp));
    sprintf(szTemp, "[%s]", sKey);

    long lKeyLength = strlen(szTemp);
    long lFieldLength = strlen(sField);

    while(!feof(fp))
    {

        memset(szBuff, 0x00, sizeof(szBuff));
        fgets(szBuff, sizeof(szBuff), fp);
        printf("szTempdd=%s\n",szTemp);
        if (memcmp(szBuff, szTemp, lKeyLength) == 0)
        {
                while(!feof(fp))
                {
                    memset(szBuff, 0x00, sizeof(szBuff));
                    fgets(szBuff, sizeof(szBuff), fp);
                    if (szBuff[0] != '[')
                    {
                        if (memcmp(szBuff, sField, lFieldLength) == 0)
                        {       
                            sprintf(sValues, "%s", &szBuff[lFieldLength+1]);
                            fclose(fp);
                            return 0;
                        }
                        continue;
                    }
                    else
                    {
                        fclose(fp);
                        return -1;/* 未找到sField*/
                        break;
                    }   
                }
        }

        continue;

    }
    fclose(fp);
    return -2;/*未找到sKey*/

}

int main()
{
    char sValues[1024];
    memset(sValues, 0x00, sizeof(sValues));
    int ret =GetCfgItem("test.ini", "PRIVATE", "HOLD", sValues);
    printf("values:%s, ret=%d\n", sValues, ret);
    return 0;
}

你可能感兴趣的:(linux,C,linux,ini,c语言)