There are a few things wrong with that.
First of all, your file consists of strings (names) as well as numbers, does it not? What do you think will happen when fscanf() is told to find a number and it sees a string? It returns without reading the string and without setting its numerical argument to anything. In other words, if this happened, table[a][b] would not be initialized, which is exactly what looks like is happening.
What can you do about this? Well, you can see if it's happening with this:
And you can "fix" it with this:Code:if(fscanf(file, "%d ", &table[a][b]) != 1) { printf("Error reading table[%d][%d]\n", a, b); }
(Note that that doesn't handle EOF, but it's good enough for now.)Code:while(fscanf(file, "%d ", &table[a][b]) != 1) { int c; do { c = getc(file); } while(c != '\n'); }
When fscanf() encounters something it didn't like, that code removes any characters up until a newline and then tries the fscanf() call again.
Of course, that doesn't fix the real problem: you're probably interested in the names, after all. So instead of just discarding them, why don't you have another look at the code that I threw at you?
Your input file consists of ten names, followed by ten lines with a name and ten numbers on them. That's exactly what this code reads. It discards the name on the last ten lines, because that same information can be obtained from the first line.Code:int x, y; for(y = 0; y < 10; y ++) { fscanf(fp, "%s", cityname[y]); } for(y = 0; y < 10; y ++) { fscanf(fp, "%s", unusedname); for(x = 0; x < 10; x ++) { fscanf(fp, "%i", &chart[y][x]); } }
Please, examine that and tell me what you don't understand about it . . . .
Also -- I think your loops are looping too often.
Most of the time, you'll want < instead of <= in loops like those.Code:for (a=0; a <= col;a++) { for (b=0; b <= row;b++)