title: How can I read struct elements from file?
body: <p>Hi I have the following structure:</p>
  
  <pre><code>struct points{
      int round[11];
      int sum;
  };
  
  struct student{
      char studid[7];
      char *s_name;
      char *f_name;
      struct points p_points;
  };
  </code></pre>
  
  <p>I am creating an array of structures, each representing an individual student. When I exit my program I will save that array to a file using the following function:</p>
  
  <pre><code>int save_file(struct student *array, int size, const char *filename){
      struct student *object=malloc(size*sizeof(struct student));
      for(int i=0;i&lt;size;i++){
          object[i].f_name=malloc(strlen(array[0].etunimi)+1);
          object[i].s_name=malloc(strlen(array[0].sukunimi+1));
          strcpy(object[i].f_name,array[0].etunimi);
          strcpy(object[i].s_name,array[0].sukunimi);
          strcpy(object[i].studid,array[0].opnro);
          object[i].points=array[0].points;
      }
      FILE * file= fopen(filename, "wb");
      if (file != NULL) {
          fwrite(object, size*sizeof(struct student), 1, file);
          fclose(file);
      }
      for(int x=0;x&lt;size;x++){
          free(object[x].etunimi);
          free(object[x].sukunimi);
      }
      free(object);
      return 1;
  }
  </code></pre>
  
  <p>When I want to modify the record and load the file I am facing a problem. I don't know how to allocate memory for object[i].f_name and object[i].s_name and I don't know the size of the array in the file. My function looks like this now:</p>
  
  <pre><code>int read_file(const char *filename){
      struct student *object2=malloc(sizeof(struct student));
      FILE * file= fopen("output", "rb");
      if (file != NULL) {
          fread(object2, sizeof(struct student), 1, file);
          fclose(file);
      }
      print_situation(object2,1);
      return 1;
  }
  </code></pre>
  
  <p>How can I check the size of the array in the file and get the lengths of f_name and s_name from the file in order to allocate the memory properly?</p>