So, I need to access a .ini file like:
[alpha]
colour=green
size=78
style=italic
[beta]
colour=black
size=94
mode=xyz
[gamma]
black=0231
blue=127
red=0x35876f
I need to find a section [likethis], then a parameter (one of the follow three) and then return its value. So, section alpha, param size, I return "78". Section gamma, param red, I return "0x35876f". section beta, param red doesn't exist.
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
i open the file with fp = fopen (sFile,"r");
, but then it gets complicated to find and return the value and I dont know if this is the best way to do it.
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
Is this ok?