title: Struct in C++ – incorrect reading from file
body: <p>My task is to create a program that does some stuff with the structures. My structure is about student's record (exam grades).</p>
  
  <p>The function in the first case in my switch creates the structure and writes it into the file. The second – reads it from the file. here's my code:</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  
  
  struct session {
      char name[50];
      int group;
      int exam1;
      int exam2;
      int exam3;
      int exam4;
      char test1[50];
      char test2[50];
  
  };
  
  void create(int);
  void show(int);
  
  
  
  int main() {
  
      int i, n;
  
      int loop = 2, num;
  
      while(loop &gt; 1) {
  
          printf("\nChoose menu item(1-4):");
          scanf("%d", &amp;num);
  
          switch (num) {
              case 1:
                  printf("The number of students:\n");
                  scanf("%d", &amp;n);
  
                  create(n);
                  break;
              case 2:
  
                  show(n);
                  break;
              case 3:
                  break;
              case 4:
                  break;
              default:
                  loop = 0;
                  break;
          }
      };
  
      return 0;
  }
  
  void create(int n) {
  
      FILE *fp;
      session *year2017 = new session[n];
  
      fp = fopen("lab8.txt", "w");
  
      if(fp == NULL) {
          printf("File can't be opened\n");exit(1);
      }
          for (int i = 0; i &lt; n; i++) {
  
              printf("Student #%d\n", i + 1);
              printf("Student's family name: ");
              while (getchar() != '\n');
  
              fgets(year2017 -&gt; name, 50, stdin);
              printf("Group number: ");
              scanf("%d", &amp;year2017-&gt;group);
              printf("1st exam grade: ");
              scanf("%d", &amp;year2017-&gt;exam1);
              printf("2d exam grade: ");
              scanf("%d", &amp;year2017-&gt;exam2);
              printf("3d exam grade: ");
              scanf("%d", &amp;year2017-&gt;exam3);
              printf("4th exam grade: ");
              scanf("%d", &amp;year2017-&gt;exam4);
              printf("First test (passed or not): ");
  
              while (getchar() != '\n');
  
              fgets(year2017 -&gt; test1, 50, stdin);
              printf("Second test (passed or not): ");
              fgets(year2017 -&gt; test2, 50, stdin);
              fwrite(year2017, sizeof(session), 1, fp);
  
  
          }
  
       fclose(fp);
  
  }
  
  void show(int n){
  
      FILE *fp;
      session *year2017 = new session[n];
  
      fp = fopen("lab8.txt", "r");
  
      while(fread(year2017,sizeof(session), 1, fp) != NULL) {
          printf("%s %d %d %d %d %d %s %s", year2017-&gt;name, year2017 -&gt; 
  group, year2017 -&gt; exam1, year2017 -&gt; exam2, year2017 -&gt; exam3, 
  year2017 -&gt; exam4, year2017 -&gt; test1, year2017 -&gt; test2);
      }
  
   fclose(fp);
  
   }
  </code></pre>
  
  <p>when I compile the code the input is the following:</p>
  
  <p><a href="http://take.ms/yoo5U" rel="nofollow noreferrer">http://take.ms/yoo5U</a></p>
  
  <p>so it eats't the name of the student in the second loop and also moves to the new line the grades and the second test.</p>
  
  <p>I use Clion and Mac OS. </p>