【问题标题】:Read .ini file and return specific value in C读取 .ini 文件并在 C 中返回特定值
【发布时间】:2015-05-02 07:36:10
【问题描述】:

所以,我需要访问一个 .ini 文件,例如:

[alpha]
colour=green
size=78
style=italic
[beta]
colour=black
size=94
mode=xyz
[gamma]
black=0231
blue=127
red=0x35876f

我需要找到一个部分 [likethis],然后是一个参数(以下三个之一),然后返回它的值。所以,部分 alpha,参数大小,我返回“78”。部分 gamma,param red,我返回“0x35876f”。部分 beta,参数 red 不存在。

char *sFile: Name of file
char *sSec:  Section where the parameter is
char *sPar:  Parametro wanted
char *sRet:  Array where I store the value
int  ilen:  Lenght of the array

我用fp = fopen (sFile,"r"); 打开文件,但是查找和返回值变得很复杂,我不知道这是否是最好的方法。

char *strAux, *strAux2;

while(!feof(fp)) //While the file doesnt end
{
    fscanf (fp,"%s",strAux); //Read a line of the file
    if(!strcmp(strAux,sSec)) //If I found the section
    {
        for(j=0; j<3; j++)
        {
            fscanf(fp,"%s",strAux); //Read a line
            strAux2 = strtok (strAux,"="); //store the param in strAux2 from the start to the =
            if (!strcmp(strAux2,sPar))
            {
                strAux2 = strtok (NULL,"\r\n"); //store in strAux2 frmo the = to end of line
                if(strlen(strAux2)>ilen)
                {
                    fclose(fp);
                    return (-3);  //Error, Lenght Value > ilen
                }else{
                    strncpy(sRet,strAux2,ilen);
                    fclose(fp);
                    return (sRet);   //Return the value
                }
            }
        }
    }           
}
fclose(fp);
return (-2);  //Error, Parameter not found

这样好吗?

【问题讨论】:

  • "I" 是 SHIFT 的英文!不,不,“我”!
  • 请参阅while (!feof(file)) is always wrong,了解为什么您的代码从一开始就是可疑的。在尝试使用 strAux 变量之前,您必须检查来自 fscanf() 的返回值。你需要分配内存来读取字符串。
  • 您的函数在成功时返回char *,但在失败时返回各种负数。也就是说,充其量是一个令人讨厌的界面。你最好总是返回一个整数——0 表示成功,负数表示失败。由于sRet 似乎是函数的输入参数,因此无需返回它来表示成功。

标签: c file ini scanf strtok


【解决方案1】:

我认为更手动的方法更容易

打开文件

搜索以[开头的行

完成后查找带有参数名称和 = 的行 (如果发现[搜索失败)

'=' 周围的额外跳过空间

FILE *f = fopen("test.ini", "r");                                          
char *sSec = "beta";                                                       
char *sPar = "size";                                                       
char *sRet = NULL;                                                         
char *p;                                                                   
char buf[1024];                                                            
bool section_search = true;                                                

while (p = fgets(buf, 1024, f)) {                                          
    if (section_search) {                                                  
        const char *q = sSec;                                              
        if (*p != '[') {                                                   
            continue;                                                      
        }                                                                  
        p++; /* read [ */                                                  
        while (*q && *p == *q) {                                           
            p++;                                                           
            q++;                                                           
        }                                                                  
        if (*q == '\0' && *p == ']') {                                     
            section_search = false;                                        
            continue;                                                      
        }                                                                  
    } else {                                                               
        const char *q = sPar;                                              

        if (*p == '[') {                                                   
            break;                                                         
        }                                                                  
        while (*q && *p == *q) {                                           
            p++;                                                           
            q++;                                                           
        }                                                                  
        if (*q != '\0')                                                    
            continue;                                                      
        while (*p == ' ') p++;                                             
        if (*p != '=')                                                     
            continue;                                                      
        p++; /*read =*/                                                    
        while (*p == ' ') p++;                                             
        sRet = p;                                                          
        break;                                                             

    }                                                                      
}                                                                          
if (sRet) {                                                                
    printf("value found %s", sRet);                                        
} else {                                                                   
    printf("value not found");                                             
}   

【讨论】:

  • 我需要搜索几个值,考虑将 ini 加载到内存中并搜索到您的结构中,可以使用 hashtable 或 trie 改进
  • 如何读取更多值,例如尺寸和颜色?我已经尝试了很多来更改代码,但它不起作用:(
  • @BiancaBalan 也许您可以将 conf 解析为哈希表,然后搜索哈希表。
【解决方案2】:

不要使用while(!feof)。相反,您可以使用while(fscanf(fp,"%s",buf) ==1),并且您可以为strtok 使用相同的标记分隔符作为= 的两个调用。在fscanf 中使用它之前,您应该为strAux 分配内存。

【讨论】:

    猜你喜欢
    • 2013-09-13
    • 1970-01-01
    • 1970-01-01
    • 2013-07-13
    • 2018-06-29
    • 2014-02-20
    • 1970-01-01
    • 1970-01-01
    • 2022-01-21
    相关资源
    最近更新 更多