title: Saving to a file in c
body: <p>I have a code which saves student numbers (stdnum) that located in a doubly linked list inside another doubly linked list into a file. I noticed that sometimes, it prints "(null)" and extra blank spaces. How do I avoid these? Here's my code:</p>
  
  <pre><code>typedef struct frn{ //structure for friend
  char stdnum[20];
  struct frn *next;
  struct frn *prev;
  }friend;
  
  typedef struct std{ //structure for student
  char stdnum[20];
  char name[20];
  char course[10];
  struct frn *friendh; 
  struct frn *friendt;
  struct std *next;
  struct std *prev;
  }student;
  
  
  
  FILE *fp1;
  student *y = h-&gt;next;
  friend *y1;
  fp1 = fopen("friends.txt", "w");
      if(y != t){
          while(y != t){
              y1 = y-&gt;friendh;
              while(y1 != NULL){ 
                  fprintf(fp1, "%s\n", y1-&gt;prev-&gt;stdnum);
                  y1 = y1-&gt;next;
              }
              y = y-&gt;next;
          }
      }
  fclose(fp1);
  </code></pre>