Note that there are some explanatory texts on larger screens.

plurals
  1. POReading Text from file into nested loop
    text
    copied!<p>I am having some trouble reading some input from a file into a multidimensional array. I have two nested for loops which print out different outputs, but I want them to print out the same output. I have been stuck on it for hours, and may just be in a mental block. The input file is a 9x9 set of numbers. My problem occurs at [0][8],[1,8], etc.. as it just copies the value from [1,0],[2,0], etc... Heres the first nested loop and its output:</p> <pre><code>for(j=0;j&lt;9;j++){ if(j!=0){ printf("%c",fgetc(file)); } for(k=0;k&lt;9;k++){ array[j][k]=fgetc(file); printf("array[%i][%i] :%c \n",j,k,array[j][k]); } printf("\n"); } </code></pre> <p>Some Output:</p> <pre><code>array[0][0] :8 array[0][1] :5 array[0][2] :2 array[0][3] :7 array[0][4] :1 array[0][5] :3 array[0][6] :9 array[0][7] :4 array[0][8] :6 array[1][0] :4 array[1][1] :3 array[1][2] :1 array[1][3] :9 array[1][4] :2 array[1][5] :6 array[1][6] :5 array[1][7] :7 array[1][8] :8 array[2][0] :9 array[2][1] :7 array[2][2] :6 array[2][3] :5 array[2][4] :4 array[2][5] :8 array[2][6] :2 array[2][7] :3 array[2][8] :1 </code></pre> <p>Here's my second nested loop which is located directly under this previous set of code. All I wanted to do was read the contents of the multi-dimensional array I just wrote to.</p> <pre><code>for(j=0;j&lt;9;j++){ for(k=0;k&lt;9;k++){ printf("array[%i][%i] :%c \n",j,k,array[j][k]); } printf("\n"); } </code></pre> <p>And here's this loop's output:</p> <pre><code>array[0][0] :8 array[0][1] :5 array[0][2] :2 array[0][3] :7 array[0][4] :1 array[0][5] :3 array[0][6] :9 array[0][7] :4 array[0][8] :4 array[1][0] :4 array[1][1] :3 array[1][2] :1 array[1][3] :9 array[1][4] :2 array[1][5] :6 array[1][6] :5 array[1][7] :7 array[1][8] :9 array[2][0] :9 array[2][1] :7 array[2][2] :6 array[2][3] :5 array[2][4] :4 array[2][5] :8 array[2][6] :2 array[2][7] :3 array[2][8] :7 </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload