c - fscanf and 2d array of integers -


disclaimer: i'm struggling beginner

my assignment read integers input txt file 2d array. when used printf's debug/test fscanf isn't reading correct values file. can't figure out why.

my code follows:

void makearray(int scores[][cols], file *ifp){     int i=0, j=0, num, numrows = 7, numcols = 14;      for(i = 0; < numrows; ++i){         for(j = 0; j < numcols; ++j){             fscanf(ifp, "%d", &num);             num = scores[i][j];         }     } }  int main(){     int scorearray[rows][cols];     int i=0, j=0;     file *ifp;     ifp = fopen("scores.txt", "r");      makearray(scorearray, ifp);      system("pause");     return 0;    } 

you assigning num (what read file) value of array scores[i][j] doing backwards. following code read arbitrary number of spaces in between each number, until reaches end of file.

void makearray(int scores[][cols], file *ifp) {     int i=0, j=0, num, numrows = 7, numcols = 14, ch;      (i=0; < numrows; ++i) {         (j=0; j < numcols; ++j) {             while (((ch = getc(ifp)) != eof) && (ch == ' ')) // eat spaces             if (ch == eof) break;                            // end of file -> break             ungetc(ch, ifp);             if (fscanf("%d", &num) != 1) break;             scores[i][j] = num;                              // store number         }     } } 

this stack overflow post covers problem in greater detail.


Popular posts from this blog