if / else的行为与预期不符

时间:2014-03-08 18:43:26

标签: c if-statement

我一直在研究一个小测试程序,它从配置文件中读取一些值到变量中,但似乎我用if / else语句得到奇怪的行为。例如,如果我运行这个并打印出已处理的变量,我得到的扇区是20000,即使它在我的cfg中定义为100000.我知道它检测到它因为如果我在strcmp()之后删除了else语句对于driveamount它的工作原理精细。但是,如果没有在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);

cfg看起来像这样:

host_serial=serial number
sectorloop=100000
driveamount=33

1 个答案:

答案 0 :(得分:3)

您的代码逻辑错误。基本上,您的代码执行此操作:

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

现在说你的输入是"sectorloop=x; driveamount=y"。第一遍将x分配给sectorloop,默认值分配给driveamount。下一行将使用默认值覆盖sectorloop,并将y分配给driveamount

你会想要这样的东西:

sectorloop = default value
driveamount = default value

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

    if string == "driveamount"
        driveamount = value from string
相关问题