I am trying to initiaze an m*n matrix line by line parsing the data with sscanf. The problem is that at the second iteration of the outer loop fgets doesn't wait for any input.
for(i = 0; i < m; ++i){
if(fgets(line, sizeof(line), fp)){
for(j = 0; j < n; ++j){
printf("j = %d i = %d n = %d line = %s\n", j, i, n, line);//DEBUG
if(sscanf(line+j*2, "%g", &mtx[i][j]) != 1){
/*Manage the error*/
}
}
}
}
Here it's the debug output:
j = 0 i = 0 n = 4 line = 1 7 3 0
j = 1 i = 0 n = 4 line = 1 7 3 0
j = 2 i = 0 n = 4 line = 1 7 3 0
j = 3 i = 0 n = 4 line = 1 7 3 0
j = 0 i = 1 n = 4 line =
And the corresponding input:
1 7 3 0
9 5 2 4
1 8 1 9
3 7 9 2