简体   繁体   中英

C code appears to misplace values read with fscanf

I wrote a C program that is behaving in a way I don't understand. I am posting this in the hope that I will learn a bit more about C. The code seems to be writing variable names into other variables using fscanf when that was not asked of it at all...

#include <stdio.h>
#include <stdlib.h>

int main() {
float BCL[6];
float varsRA[23],varsLA[23],varsBB[23],varsPV[23];  
FILE *inputfil;
int i;

inputfil = fopen ("file.txt", "rt");        
for(i=0;i<24;i++) {
    fscanf(inputfil,"%f %f %f %f\n", &(varsRA[i]), &(varsLA[i]), &(varsBB[i]), &(varsPV[i]));
}

i=23;
printf("vars%d:%f %f %f %f\n",i,varsRA[i], varsLA[i], varsBB[i], varsPV[i]);

i=0;
while(!feof(inputfil)) {
    fscanf(inputfil,"%f ",&(BCL[i]));
    i++;
}

i=23;
printf("vars%d:%f %f %f %f\n",i,varsRA[i], varsLA[i], varsBB[i], varsPV[i]);

fclose(inputfil);
return 0;
}

The outcome is:

vars23:-66.336823 -68.164223 -57.850136 -60.762585
vars23:-66.336823 -68.164223 -57.850136 177.000000

Why was the last value of varsPV changed to 177, which is the first value of BCL, when I read the rest of the file?!

Many thanks for your help, Marta

The program has undefined behaviour as it is accessing beyond the bounds of the arrays:

for(i=0;i<24;i++) {
    fscanf(inputfil,"%f %f %f %f\n", &(varsRA[i]), &(varsLA[i]), &(varsBB[i]), &(varsPV[i]));
}

Array indexes run from 0 to N - 1 , where N is the number of elements in the array. The arrays are defined with 23 elements, meaning valid indexes are from 0 to 22 . Change the loop terminating condition to i < 23 or use a compile time constant to define the array size and the terminating condition to avoid duplicating that piece of information.

There are other out of bounds accesses in the program and this loop needs protection from going out of bounds:

i=0;
while(!feof(inputfil)) {              /* i < 6 required */
    fscanf(inputfil,"%f ",&(BCL[i]));
    i++;
}

Always check the return value of fscanf() to ensure assignments were actually made. fscanf() returns the number of assignments made:

for (i = 0; i < 6 && 1 == fscanf(input, "%f", &BCL[i]); i++);

Your arrays aren't being defined big enough. The array varsRA[23] only contains 23 elements numbered 0 through 22 . You're reading in 24 elements numbered 0 through 23 . Change your array definitions to the following and you should be good to go.

float varsRA[24],varsLA[24],varsBB[24],varsPV[24];

Array indices in C are zero-based.

float varsRA[23],varsLA[23],varsBB[23],varsPV[23]

Those arrays have 22 as their last valid index , which makes 23 elements. That's why

for(i=0;i<24;i++)

writes one extra element to each of them. What you get is undefined behavior (so there is nothing reasonable to expect), but your particual kind of undefined behavior is overwriting a piece of another array (and probably some random memory location).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM