title: Read file and write/sort in array of struct
body: <p>I am trying to write a program which reads a file, stores the data in an array of struct, calculates volume and is written to a new file sorted by volume. The program compiles and writes to the new file, but it is not sorting by volume and weight prints 0.000000 for each weight.</p>
  
  <p><strong>EDIT: Everything works except sorting by volume</strong></p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #define PI 3.14159265
  
  struct cylinder {
      double radius;
      double height;
      double weight;
      double volume;
  };
  
  void selection_sort(struct cylinder my_cylinders[], int n);
  
  int main() {
      FILE *cFileIn, *cFileOut;
      double radius, height, weight, volume;
  
      struct cylinder my_cylinders[100];
  
      cFileIn = fopen("cylinders.txt", "r");
      if (cFileIn == NULL) {
          printf("Error opening file \n");
          return 1;
      }
  
      cFileOut = fopen("sorted_cylinders.txt", "w");
      if (cFileOut == NULL) {
          printf("Error opening file \n");
          return 1;
      }
  
      fprintf(cFileOut, "#    Radius           Height          Volume          Weight\n");
  
      int i, counter = 0;
      for (i = 0; i &lt; 6; i++) {   
          while (fscanf(cFileIn, "%lf, %lf, %lf, %lf\n", &amp;radius, &amp;height, &amp;volume, &amp;weight) != EOF) {
              my_cylinders[counter].radius = radius;
              my_cylinders[counter].height = height;
              my_cylinders[counter].volume = volume;
              my_cylinders[counter].weight = weight;
  
              volume = PI * radius * radius * height; 
              fprintf(cFileOut, "%-d\t  %-12.6lf\t  %-12.6lf\t  %-12.6lf\t   %-12.6lf\t \n", 
                      counter, radius, height, volume, weight);
              counter++;      
          }
      }
  
      selection_sort(my_cylinders, counter);
      printf("File sorted_cylinders.txt written \n");
  
      fclose(cFileIn);
      fclose(cFileOut);
      return 0;
  }
  
  void selection_sort(struct cylinder my_cylinders[], int n) {
      int i = 0, j = 0, min;
      struct cylinder temp;
      for (i = 0; i &lt; n - 1; ++i) {
          min = i;
          for (j = i + 1; j &lt; n; ++j) {
              if (my_cylinders[j].volume &lt; my_cylinders[min].volume) {
                  min = j;
              }
          }
          temp = my_cylinders[i];
          my_cylinders[i] = my_cylinders[min];
          my_cylinders[min] = temp;
      }   
  }
  </code></pre>
  
  <p>Output:</p>
  
  <pre><code>#    Radius           Height          Volume          Weight
  0     2.400000        12.000000       217.146884       15.000000    
  1     18.200000       14.200000       14776.820321     25.900000    
  2     22.800000       10.600000       17311.130546     4.500000     
  3     3.500000        2.500000        96.211275        15.800000    
  4     6.000000        1.000000        113.097335       2.900000     
  5     21.000000       1.000000        1385.442359      100.000000   
  </code></pre>