0

I'm trying to get values into a 2D float array from a file. Everything works fine except data[c][6] is equal to data[c+1][0] when I print it out. Here's the code:

while(!feof(fp)){
    for (int c = 0; c < 2000; c++){
        fscanf(fp,"%s",str);
        sscanf(str," %[^,] %*c %[^,] %*c %[^,] %*c %f %*c %f %*c %f %*c %[^,] %*c %f %*c",names[c],temp2,temp3,&data[c][2],&data[c][3],&data[c][4],global,&data[c][6]);
        //in file values seperated by ',' this is why I use this.//
        if(check_duplication(temp2,genres,12) != 1){ 
            strcpy(genres[i],temp2);
            i ++;
        }

        if(check_duplication(temp3,platforms,10) != 1){
            strcpy(platforms[x],temp3);
            x ++;
        }

        data[c][0] = find_string(temp2,genres,12); //take the location of genres and platforms.//
        data[c][1] = find_string(temp3,platforms,10);
        data[c][5] = strtod(global, &temp);
    }
}

Here's some of my output:

1996) 8.00 5.00 2005.00 0.04 0.01 0.05 5.00

1997) 5.00 5.00 2005.00 0.19 0.07 0.26 3.00

1998) 3.00 6.00 2005.00 0.19 0.05 0.24 3.00

1999) 3.00 6.00 2005.00 0.05 0.01 0.07 0.00

2000) 0.00 6.00 2005.00 0.26 0.07 0.00 11.00

2001) 11.00 0.00 2005.00 0.00 0.02 0.02 8.30

As you can see, the printed values for data[1996][6] and data[1997][0] are the same. The data[c][0]'s are the correct values, but the data[c][6]'s are not what I want.

When I print with printf("%f",&data[c][6]) in the loop, it gives the correct value, but when I print the whole array, the values are wrong.

John Bollinger
  • 160,171
  • 8
  • 81
  • 157
sena
  • 3
  • 1
  • 2
  • 1
    Check the return value of `fscanf()` before using `str` in the next line. Check the return value of `sscanf()` before using `names[c],temp2,temp3,&data[c][2] ....`. See[Why is “while ( !feof (file) )” always wrong?](https://stackoverflow.com/questions/5431941/why-is-while-feof-file-always-wrong) – chux - Reinstate Monica Apr 22 '20 at 13:02
  • @user3121023 oh thank you, I've tried a lot of things but couldn't see something so small like that. It works now. – sena Apr 22 '20 at 13:05
  • @user3121023 Is likely correct. For a more complete feedback, we really need a [mcve]. – chux - Reinstate Monica Apr 22 '20 at 13:05

0 Answers0