title: How can I make fread work properly in C? It fills my dynamic array with zeroes
body: <p>I'm writing a c program that needs to read a binary file, put it into the array of structures and then sort this array.
  Structure:</p>
  <pre><code>#define NCHAR 64
  
  typedef
      struct data_
      {
          char country[NCHAR];
          long int population;
          float world_share;
      }
  data;
  </code></pre>
  <p>All this happens in one separate function,that uses <code>freed()</code> to get the array form the file. And this function does not really works.
  What i expect to see:</p>
  <pre><code>CountryA        100    10%
  CountryB        200    20%
  </code></pre>
  <p>What i actually i get:</p>
  <pre><code>0        0    0%
  0        0    0%
  </code></pre>
  <p>Can you please take a look what i am doing wrong? I've tried rewriting my code and could and searching for uses of <code>fread()</code> function but it seems like i am using it right.</p>
  <pre><code>void SortAndDisplayASC( void )//main
  {
      FILE * worldData;
      worldData = fopen(&quot;world.dat&quot;, &quot;rb&quot;);
      if(worldData == NULL)
      {
          printf(&quot;NO FILE CAN BE ACSESSED\n&quot;);
          exit(1);
      }
      long size = fsize(worldData);
      int amount = size / sizeof(data);
      data * countries;
      countries = (data *) malloc(size);
      fread(countries, sizeof(data), amount, worldData); //PROBLEM HERE!
      sortByPopulation(countries, amount);
      for(int i = 0; i &lt; amount;  i ++)
      {
          PrintData(countries[i]);
      }
      free(countries);
      fclose(worldData);
  }
  </code></pre>
  <p>If you will to help me, i'll share the whole code.</p>
  <h2>Post Scriptum:</h2>
  <p>Hello to all who happen to stumble in this question.
  At the moment of writing it was my C assignment.
  I have found and fixed the bug in my code back then and forgot to post it.
  Now I need to post a new question and this one caught my attention. Here is the source code of this question. This version should work fine.</p>
  <pre class="lang-c prettyprint-override"><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;string.h&gt;
  
  #define NCHAR 64
  #define WPOP 7800000000L
  
  typedef
      struct data_
      {
          char country[NCHAR];
          long int population;
          float world_share;
      }
  data;
  
  void menu(int option);
  //1st choise
  void EnterAndWrite( void );
  void ScanData(data * subject);
  //2nd choise
  void ReadTheCountry( void );
  void PrintData(data subject);
  //3rd choise
  void SortAndDisplayASC( void );
  long fsize(FILE * f);
  void sortByPopulation(data *array, int n);
  void dataArrayItemSwap(data *array, int a, int b);
  //4th choise
  void SortAndDisplayDSC( void );
  void dataArrayFlip(data *array, int length);
  //5th choise
  void SearchForCountry( void );
  void findByName(data * array, int n);
  int myStrcmp(char * s1, char * s2);
  //6th choise
  void CompareDataWithWPOP();
  
  int main(void)
  {
      int option;
      do
      {
          printf( &quot; (*) Here you will be able do persform several actions over the file world.dat\n\n&quot;
                  &quot; (*) Enter 1 to enter the data of new country that will be added to file.\n&quot;
                  &quot; (*) Enter 2 to enter the position of the country in the file that you want to see.\n&quot;
                  &quot; (*) Enter 3 to show the database sorted by population in asceding order.\n&quot;
                  &quot; (*) Enter 4 to show the database sorted by population in desceding order.\n&quot;
                  &quot; (*) Enter 5 to search file for a specific country by its name or it's first lettes.\n&quot;
                  &quot; (*) Enter 6 to see total population stored in DB and compare it with world population.\n&quot;
                  &quot; (*) Enter 0 to teminate.\n&quot;);
          scanf(&quot;%d%*c&quot;, &amp;option);
          menu(option);
  
      }while(option &gt; 0);
      return 0;
  }
  
  void menu(int option)
  {
      switch(option)
      {
          case 1:
          {
              EnterAndWrite();
              break; 
          }
          case 2:
          {
              ReadTheCountry();
              break; 
          }
          case 3:
          {
              SortAndDisplayASC();
              break; 
          }
          case 4:
          {
              SortAndDisplayDSC();
              break; 
          }
          case 5:
          {
              SearchForCountry();
              break; 
          }
          case 6:
          {
              CompareDataWithWPOP();
              break; 
          }
      }
  }
  
  //6th choise
  void CompareDataWithWPOP()
  {
      FILE * worldData;
      worldData = fopen(&quot;world.dat&quot;, &quot;rb&quot;);
      if(worldData == NULL)
      {
          printf(&quot;NO FILE CAN BE ACSESSED\n&quot;);
          exit(1);
      }
      long size = fsize(worldData);
      int amount = size / sizeof(data);
      data * countries;
      countries = (data *) malloc(size);
      fread(countries, sizeof(data), amount, worldData);
      long int sum = 0;
      for(int i = 0; i &lt; amount; i ++)
      {
          sum += countries[i].population;
      }
      float share = (float)sum / WPOP;
      printf(&quot;%ld %.2f%%\n&quot;, sum, share*100);
      free(countries);
      fclose(worldData);
  }
  
  //5th choise
  void SearchForCountry( void )
  {
      FILE * worldData;
      worldData = fopen(&quot;world.dat&quot;, &quot;rb&quot;);
      if(worldData == NULL)
      {
          printf(&quot;NO FILE CAN BE ACSESSED\n&quot;);
          exit(1);
      }
      long size = fsize(worldData);
      int amount = size / sizeof(data);
      data * countries;
      countries = (data *) malloc(size);
      fread(countries, sizeof(data), amount, worldData);
      findByName(countries, amount);
      free(countries);
      fclose(worldData);
  }
  void findByName(data * array, int n)
  {
      char request[NCHAR];
      fgets(request, NCHAR-1, stdin);
      request[strlen(request)-1] = '\0';
      for(int i = 0; i &lt; n; i ++)
      {
          if(myStrcmp(request, array[i].country) == 0)
          {
              PrintData(array[i]);
          }
      }
  }
  int myStrcmp(char * s1, char * s2)
  {
      int n = (strlen(s1) &lt; strlen(s2)) ? strlen(s1) : strlen(s2);
      char *temp = (char *) calloc(n+1, 1);
      for(int i = 0; i &lt; n; i ++)
      {
          temp[i] = (strlen(s1) &gt; strlen(s2)) ? s1[i] : s2[i];
      }
      return (strlen(s1) &gt; strlen(s2)) ? strcmp(temp, s2) : strcmp(s1, temp);
  }
  
  //4th choise
  void SortAndDisplayDSC( void )//main
  {
      FILE * worldData;
      worldData = fopen(&quot;world.dat&quot;, &quot;rb&quot;);
      if(worldData == NULL)
      {
          printf(&quot;NO FILE CAN BE ACSESSED\n&quot;);
          exit(1);
      }
      long size = fsize(worldData);
      int amount = size / sizeof(data);
      data * countries;
      countries = (data *) malloc(size);
      fread(countries, sizeof(data), amount, worldData); //PROBLEM HERE!
      sortByPopulation(countries, amount);
      dataArrayFlip(countries, amount);
      for(int i = 0; i &lt; amount;  i ++)
      {
          PrintData(countries[i]);
      }
      free(countries);
      fclose(worldData);
  }
  void dataArrayFlip(data *array, int length)
  {
      data * temp;
      temp = (data *) calloc(length, sizeof(data));
      for(int i = 0; i &lt; length; i++) temp[i] = array[(length - i) - 1];
      for(int i = 0; i &lt; length; i++) array[i] = temp[i];
      free(temp);
  }
  
  //3rd choise
  void SortAndDisplayASC( void )//main
  {
      FILE * worldData;
      worldData = fopen(&quot;world.dat&quot;, &quot;rb&quot;);
      if(worldData == NULL)
      {
          printf(&quot;NO FILE CAN BE ACSESSED\n&quot;);
          exit(1);
      }
      long size = fsize(worldData);
      int amount = size / sizeof(data);
      data * countries;
      countries = (data *) malloc(size);
      fread(countries, sizeof(data), amount, worldData); //PROBLEM HERE!(no problem now)
      sortByPopulation(countries, amount);
      for(int i = 0; i &lt; amount;  i ++)
      {
          PrintData(countries[i]);
      }
      free(countries);
      fclose(worldData);
  }
  long fsize(FILE * f)
  {
      long lenght;
      fseek(f, 0, SEEK_END);
      lenght = ftell(f);
      fseek(f, 0, SEEK_SET);
      return lenght;
  }
  void sortByPopulation(data *array, int n)
  {
      //sorts countries in asceding order by population
      for(int i = 0; i &lt; n-1; i ++)
      {
          for(int j = i+1; j &lt; n; j ++)
          {
              if(array[i].population &gt; array[j].population)
              {
                  dataArrayItemSwap(array, i, j);
              }
          }
      }
  }
  void dataArrayItemSwap(data *array, int a, int b)
  {
      //changes places of 2 items in an array
      data temp = array[b];
      array[b] = array[a];
      array[a] = temp;
  }
  
  //2nd choise
  void ReadTheCountry( void )//main
  {
      FILE * worldData;
      worldData = fopen(&quot;world.dat&quot;, &quot;rb&quot;);
      //test if file is a true thing
      if(worldData == NULL)
      {
          printf(&quot;NO FILE CAN BE ACSESSED\n&quot;);
          exit(1);
      }
      int n;
      //get the country number to read(start form 0)
      scanf(&quot;%d%*c&quot;, &amp;n);
      //caculating the position
      int offset = sizeof(data) * n;
      //moving to that position
      fseek(worldData, offset, SEEK_SET);
      data Country;
      //reading data form that position
      fread(&amp;Country, sizeof(data), 1, worldData);
      PrintData(Country);
      fclose(worldData);
  }
  void PrintData(data subject)
  {
      //prints name, population and world share and starts a new line
      printf(&quot;%-16s\t%10ld\t\t%5.2f%%\n&quot;, subject.country, subject.population, subject.world_share * 100);
  }
  
  //1st choise
  void EnterAndWrite( void )//main
  {
      FILE * worldData;
      worldData = fopen(&quot;world.dat&quot;, &quot;ab&quot;);
      //test if file can be acsessed
      if(worldData == NULL)
      {
          printf(&quot;NO FILE CAN BE ACSESSED\n&quot;);
          exit(1);
      }
      //get data to append the file
      data Country;
      ScanData(&amp;Country);
      //go to the end of file
      fseek(worldData, 0, SEEK_END);
      fwrite(&amp;Country, sizeof(data), 1, worldData);
      fclose(worldData);
  }
  void ScanData(data * subject)
  {
      //get name
      fgets(subject-&gt;country, NCHAR-1, stdin);
      subject-&gt;country[strlen(subject-&gt;country)-1] = '\0';
      //get population
      scanf(&quot;%ld%*c&quot;, &amp;subject-&gt;population);
      //find world share
      subject-&gt;world_share = (float)subject-&gt;population / WPOP;
      //end of the function;
  }
  
  </code></pre>
  <p>This piece of code worked with world.dat file, where structures describing countries were saved in binary. I have converted it to base64 and you are free to construct it back.</p>
  <pre><code>Q2hpbmEAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAD/////AAAAAAAAAAAAAAAAqIJW3/5/AAAQ9+BhGX8AAIBWylUAAAAAF/U8PgAAAABJbmRpYQAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAP////8AAAAAAAAAAAAAAACoglbf/n8AABD34GEZfwAAITJBUgAAAAB4KzU+AAAAAFVuaXRlZCBTdGF0ZXMAAAAAAAAAAAAAAAAAAAAAAAAA/////wAAAAAAAAAAAAAAAKiCVt/+fwAAEPfgYRl/AAAbs7oTAAAAAJ/RLT0AAAAASW5kb25lc2lhAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAD/////AAAAAAAAAAAAAAAAqOKfpf9/AAAQd1Kbw38AAJ+jTRAAAAAAjaIPPQAAAABQYWtpc3RhbgAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAP////8AAAAAAAAAAAAAAACo4p+l/38AABB3UpvDfwAAtIwqDQAAAABc/uc8AAAAAEJyYXppbAAAAAAAAAA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AAAAAAAAAAD/////AAAAAAAAAAAAAAAAqOKfpf9/AAAQd1Kbw38AADuZVgUAAAAA9x88PAAAAABUdXJrZXkAAABlcwAAAAAAAAAAAAAAAAAAAAAAAAAAAP////8AAAAAAAAAAAAAAACo4p+l/38AABB3UpvDfwAAe+kGBQAAAADBJzE8AAAAAElyYW4AAAAAAGVzAAAAAAAAAAAAAAAAAAAAAAAAAAAA/////wAAAAAAAAAAAAAAAKjin6X/fwAAEHdSm8N/AAB1oQEFAAAAAKNtMDwAAAAAR2VybWFueQAAZXMAAAAAAAAAAAAAAAAAAAAAAAAAAAD/////AAAAAAAAAAAAAAAAqOKfpf9/AAAQd1Kbw38AAAZx/gQAAAAAP/0vPAAAAABUaGFpbGFuZAAAcwAAAAAAAAAAAAAAAAAAAAAAAAAAAP////8AAAAAAAAAAAAAAACo4p+l/38AABB3UpvDfwAAKhApBAAAAAChnRI8AAAAAA==
  </code></pre>
  <p>MD5 of original file:</p>
  <pre><code>c2c0571518b47973f719f5dba96621dc
  </code></pre>
  <p>SHA256 of original file:</p>
  <pre><code>10ba4a9bc275b2c12f1b1f52fefc124d83df4485fe24c40ffcf5ca97830f1721
  </code></pre>
  <p>So, as far as I remember, there was a bug in fsize function that returned wrong size of a file in one weird case. This version should be with out the bug.
  <br>
  Thanks everyone who tried to help me!</p>