title: Reading a 2D array from a file in C
body: <p>I'm attempting to create a program to read a 2D array from a file and then print it out. </p>
  
  <p>The file is set out so that the first line has the number of rows and then the number of columns. After that the array is plotted. An example is as follows:</p>
  
  <pre><code>3 5
  10.4 15.1 18.5 13.3 20.8
  76.5 55.3 94.0 48.5 60.3
  2.4 4.6 3.5 4.6 8.9
  </code></pre>
  
  <p>My problem is that i only know how to read the first element of each line using fgets and sscanf so the following numbers are ignored.</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;string.h&gt;
  
  int main(int argc, char* argv[]){
  
  FILE* f = fopen("plottestdata", "r");
  char size[20];
  int height, width,ii=0,cc,jj,kk;
  float array[100][100];
  char horiz[500];
  
  if(fgets(size, 20, f)!= NULL){
  
      sscanf(size,"%d %d", &amp;height, &amp;width);
      printf("%d %d\n",height, width);
  }
  while(fgets(horiz, 500, f)!=NULL)
  {
  
      if(ii&lt;height)
      {
          for(cc=0;cc&lt;width;cc++)
          {
          sscanf(horiz, "%f", &amp;array[ii][cc]);
          }
      ii++;
      }
  }
  for(jj=0;jj&lt;width;jj++)
      {
          for(kk=0;kk&lt;height;kk++)
          {
          printf("%f ", array[jj][kk]);
          }
      }
  fclose(f);
  return 0;
  }
  </code></pre>
  
  <p>This gives me an output of the first element of each line repeated multiple times and I understand why but am unsure how to fix it. The file it is reading from is actually a 20x20 array although set out in the same format as the example.</p>
  
  <p>Also, i have left out my error checking for the sake of trying to shortening a long question.</p>