title: Read wrong values from binary file
body: <p>I want to take an array of structs so I can sort it by name and write it on a txt file. But it takes wrong values like strange symbols or numbers. Anyone knows what is wrong?</p>
  
  <pre><code>typedef struct candidato Candidato;
  
  struct candidato {
      char inscr[10];
      char nome[44];
      int periodo;
      int posicao;
      char curso[30];
  };
  
  FILE *fp = fopen(filename, "rb");
  
  if (fp == NULL)
  {
      fprintf(stderr, "Failed to open file %s for reading\n", filename);
      return ;
  }
  fseek(fp, 0, SEEK_END);
  size_t sz = ftell(fp);
  int ncand = sz/sizeof(Candidato);
  rewind(fp);
  Candidato *arr = malloc(sz);
  if (arr == 0)
  {
      fprintf(stderr, "Failed to allocate %zu bytes memory\n", sz);
      return ;
  }
  printf("%d \n",ncand);
  int i;
  int cont;
  for (i = 0; fread(&amp;arr[i], sizeof(Candidato), 1, fp) == 1; i++){
      printf("%s\n",arr[i].nome); //test if it got what I want
  }
  fclose(fp);
  </code></pre>