title: Create a student record using structures and pointers in C language
body: <p>I am having issues with trying to build a student record. The <code>struct</code> has four variables which are <code>id</code>, <code>name</code>, <code>age</code>, and <code>GPA</code>. I am supposed to read in a file and dynamically allocate memory for a new student <code>struct</code> which is suppose to hold a data set (student record). Then finally I'm supposed to print the total number of students that were successfully created from the file, <code>average age</code> (as float or double), and <code>average GPA</code> (as float or double). As you can see I am very lost and would appreciate any help in coding this project. Will be forever grateful! Here is what I have so far:</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  
  typedef struct student
  {
      int id;
      char name[255];
      int age;
      float gpa;
  }student;
  
  int makeStudent(FILE *fpin, student *students)
  {
      int num_Students = 0;
      int rowNum = 0;
  
      students = (student *)malloc(sizeof(student) * 50);
  
      if((fscanf(fpin, "%d", students-&gt;id) &lt; 0))
      {
          printf("Cannot create student record from file row # %d: id is invalid", rowNum);
          exit(EXIT_FAILURE);
      }
  
      if((fscanf(fpin, "%s", students-&gt;name) != 1))
      {
          printf("Cannot create student record from file row # %d: name is invalid", rowNum);
          exit(EXIT_FAILURE);
      }
  
      if((fscanf(fpin, "%d", students-&gt;age) &lt; 0))
      {
          printf("Cannot create student record from file row # %d: age is invalid", rowNum);
          exit(EXIT_FAILURE);
      }
  
      fscanf(fpin, "%d", students-&gt;gpa);
      if(students-&gt;gpa &lt; 0.0 || students-&gt;gpa &gt; 4.0)
      {
          printf("Cannot create student record from file row # %d: GPA is invalid", rowNum);
          exit(EXIT_FAILURE);
      }
      num_Students++;
      rowNum++;
      return 1;
  }
  
  
  int main()
  {
      FILE *fpin;
      char name[255];
      int size = 0;
  
      fpin = fopen("student.txt", "r");
  
      student studentRecord[255];
      while(fgets(name, 255, fpin) != NULL)
          printf("%s", name);
  
      /*while(1)
      if(!makeStudent(fpin, &amp;studentRecord[size]))
      break;
      ++size;
      if(fpin == NULL)
      {
      perror("Error while opening the file.\n");
      exit(EXIT_FAILURE);
      }
      */
  
      fclose(fpin);
      return 0;
  }
  </code></pre>
  
  <p>FILE DATA BELOW:</p>
  
  <pre><code>1
  Bob Smith
  24
  3.5
  2
  Jill Williams
  23
  3.6
  3
  Tom Jones
  32
  2.4
  4
  Julie Jackson
  21
  3.1
  5
  Al Brown
  23
  3.35
  6
  Juan Garcia
  22
  3.4
  -7
  Melissa Davis
  20
  3.2
  8
  Jack Black
  44
  1.1
  </code></pre>