C 如果/否则不';I don’我没有表现得像预期的那样

C 如果/否则不';I don’我没有表现得像预期的那样,c,if-statement,C,If Statement,我一直在开发一个小测试程序,它将配置文件中的一些值读取到变量中,但似乎我在if/else语句中出现了奇怪的行为。例如,如果我运行它并打印出处理过的变量,我得到的sectorloop是20000,即使它在我的cfg中定义为100000。我知道它会检测到它,因为如果在driveamount的strcmp()之后删除else语句,它就可以正常工作。但是,如果test.cfg中没有定义回退,这将打破回退 int sectorloop,driveamount,q,j,p; char

我一直在开发一个小测试程序,它将配置文件中的一些值读取到变量中,但似乎我在if/else语句中出现了奇怪的行为。例如,如果我运行它并打印出处理过的变量,我得到的sectorloop是20000,即使它在我的cfg中定义为100000。我知道它会检测到它,因为如果在driveamount的strcmp()之后删除else语句,它就可以正常工作。但是,如果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);

你的代码逻辑是错误的。基本上,您的代码执行以下操作:

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

你会想要这样的东西:

host_serial=serial number
sectorloop=100000
driveamount=33
sectorloop = default value
driveamount = default value

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

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

你的代码逻辑是错误的。基本上,您的代码执行以下操作:

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

你会想要这样的东西:

host_serial=serial number
sectorloop=100000
driveamount=33
sectorloop = default value
driveamount = default value

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

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

你的代码逻辑是错误的。基本上,您的代码执行以下操作:

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

你会想要这样的东西:

host_serial=serial number
sectorloop=100000
driveamount=33
sectorloop = default value
driveamount = default value

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

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

你的代码逻辑是错误的。基本上,您的代码执行以下操作:

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

你会想要这样的东西:

host_serial=serial number
sectorloop=100000
driveamount=33
sectorloop = default value
driveamount = default value

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

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

为什么不让代码可读?i、 每生产线一次手术?有人知道眼睛出血的快速修复方法吗?通常不会为了容易理解的手术而这么做。。。修正了我猜
while((cfgstring[j][q]=fgetc(configfp))!=EOF&&qAlso不是
j
变量(它决定何时停止q上的循环)在内部for循环中被覆盖?这不太好。为什么不让代码可读?例如,每行一个操作?有人知道眼睛出血的快速修复方法吗?通常不会为了易于理解的操作而这样做…修复了我猜
而((cfgstring[j][q]=fgetc(configfp))!=EOF&&qAlso,
j
变量不是吗(这决定了何时停止q上的循环)在内部for循环中被覆盖?这不太好。为什么不让代码可读?即每行一个操作?有人知道眼睛出血的快速修复方法吗?通常不会为了易于理解的操作而这样做…修复了我猜
而((cfgstring[j][q]=fgetc(configfp))!=EOF&&qAlso,
j
变量(决定何时停止q上的循环)不是在内部for循环中被覆盖了吗?这不太好。为什么不让代码可读?例如,每行一个操作?有人知道眼睛出血的快速修复方法吗?通常不会为了易于理解的操作而这样做…修复了我想
while((cfgstring[j][q] =fgetc(configfp))!=EOF&&qAlso,
j
变量(决定何时停止q上的循环)不是在内部for循环中被覆盖了吗?这不太好。