user2882307
user2882307

Reputation:

if/else doesn't behave as expected

I have been working on a little test program which reads some values from a config file into variables however it seems I get odd behavior with if/else statements. For instance if I run this and print out the processed variables I get that sectorloop is 20000 even though it is defined in my cfg as 100000. I know it detects it because If I remove the else statement after the strcmp() for driveamount it works fine. This would however break the fallback if it isn't defined in the test.cfg...

int             sectorloop,driveamount,q,j,p;
char            cfgstring[3][128],sys_serial[128];

/* 
the analyzing is broken up into 6 steps:
1:   read test.cfg data into an 2d array of chars with newlines as delimiter.
2.   go through each value in the array and look for the '=' this is the delimiter for the meaning=value.
3.   overwrite '=' to a string terminating character '\0' C thinks the meaning specifier is the entire string while still keeping the value in memory allthough hidden
4.   use string compare to match to meaning to the desired string.
5.   if there is a match move the value(after the '=' in the config) to the beginning of the array. overwriting the meaning as it isn`t needed anymore
6.   now we have a string which we know what meaning it belonged to and can use it in the program, sometimes it needs to be converted to int or whatever
*/

FILE *configfp=fopen("test.cfg","r");
if (configfp==NULL) {
    sectorloop=50000;
    driveamount=27;
    sys_serial[0]='\0';
} else {
    q=j=0;while ((cfgstring[j][q]=fgetc(configfp))!=EOF&&q<128&&p<3) {
        if (cfgstring[j][q]==10||cfgstring[j][q]==13) {
            cfgstring[j][q]='\0';
            q=0;
            j++;
        }
        else {
            q++;
        }
    }
    cfgstring[j][q]='\0';

    for (q=0;q<=j;q++) {
        p=0;while (cfgstring[q][p]!='='&&cfgstring[q][p]!='\0'&&p<128) {
            p++;
        }
        cfgstring[q][p]='\0';

        if ((strcmp(cfgstring[q],"host_serial"))==0) {
            j=0;while (cfgstring[q][j+p+1]!='\0') {
                cfgstring[q][j]=cfgstring[q][j+p+1];
                j++;
            }
            cfgstring[q][j]='\0';
            strcpy(sys_serial,cfgstring[q]);
        }

        if ((strcmp(cfgstring[q],"sectorloop"))==0) {
            j=0;while (cfgstring[q][j+p+1]!='\0') {
                cfgstring[q][j]=cfgstring[q][j+p+1];
                j++;
            }
            cfgstring[q][j]='\0';
            if ((sectorloop=atoi(cfgstring[q]))==0) {
                sectorloop=50000;
            }
        } else {
            sectorloop=20000;
        }

        if ((strcmp(cfgstring[q],"driveamount"))==0) {
            j=0;while (cfgstring[q][j+p+1]!='\0') {
                cfgstring[q][j]=cfgstring[q][j+p+1];
                j++;
            }
            cfgstring[q][j]='\0';
            if ((driveamount=atoi(cfgstring[q]))==0) {
                driveamount=27;
            }  
        } else {
            driveamount=27;
        }
    }
}
fclose(configfp);

the cfg look like this:

host_serial=serial number
sectorloop=100000
driveamount=33

Upvotes: 0

Views: 105

Answers (1)

M Oehm
M Oehm

Reputation: 29116

Your code logic is wrong. Basically, your code does this:

for each config string:
    if string == "sectorloop"
        sectorloop = value from string
    else
        sectorloop = default value

    if string == "driveamount"
        driveamount = value from string
    else
        driveamount = default value

Now say your input is "sectorloop=x; driveamount=y". The first pass will assign x to sectorloop and the default value to driveamount. The next pass will overwrite sectorloop with the default and assign y to driveamount.

You'll want something like this:

sectorloop = default value
driveamount = default value

for each config string:
    if string == "sectorloop"
        sectorloop = value from string

    if string == "driveamount"
        driveamount = value from string

Upvotes: 2

Related Questions