title: C pointer, struct and fwrite
body: <p>I'm currently trying to write and read a simple linked list into a file, but it doesn't really seem to work and I'm not sure if it is even possible.</p>
  
  <pre><code>typedef struct flug
  {
      int            flugnummer;
      char            flugziel[50];
      enum TAG        flugtag;
      int            flugzeit_stunde;
      int            flugzeit_minute;
      int            gateway;
      char            status[10];
      struct flug    *next;
  }FLUG;
  
  typedef FLUG *ELEM_ZGR;
  </code></pre>
  
  <p>I fear the problem might be that I'm not only writing chars, but int .. and especially enums, too.</p>
  
  <pre><code>int fluege_sichern() {
  ELEM_ZGR curr;
  
  FILE *fp;
  char* tag;
  
  curr = first;
  
  if (fopen_s(&amp;fp, datei,"a+b") != 0) {
      printf("\nDatei %s nicht zum Anhaengen zu oeffnen",datei);
      PAUSE;
      exit(1);
  }
  
  for(curr = first; curr != NULL; curr = curr-&gt;next) {
      fwrite(curr, sizeof(FLUG), 1, fp);
  }
  
      fclose(fp);
      return 1;
  }
  </code></pre>
  
  <p>This is supposed to be the function to write the elements into a file as binaries. I don't get any errors in this function.</p>
  
  <pre><code>void fluege_laden() {
  ELEM_ZGR curr;
  FILE *fp;
  int i = 0;
  
  if (fopen_s(&amp;fp, datei,"rb") != 0) {
      printf("\nDatei %s nicht zum Lesen zu oeffnen",datei);
      PAUSE;
      exit(1);
  }
  
  while(1) {
       fread(&amp;curr, sizeof(FLUG), 1, fp);
       printf("\n%d", curr-&gt;flugnummer);
       //fluege_sortieren(curr);
  }
  }
  </code></pre>
  
  <p>I get the error when I try to print out curr->flugnummer. </p>
  
  <p>Ok so I changed the two functions a little, but it still doesn't seem to work. I'm sorry, I don't seem to get it...</p>
  
  <p>[EDIT2] Another version that doesn't work:</p>
  
  <pre><code>void fluege_laden() {
      ELEM_ZGR curr;  // &lt;&lt;&lt;- allocate an actual struct here rather than just a pointer
      FILE *fp;
  
      int i = 0;
      curr = first;
  
      if (fopen_s(&amp;fp, datei,"rb") != 0) {
          printf("\nDatei %s nicht zum Lesen zu oeffnen",datei);
          PAUSE;
          exit(1);
      }
  
      while(1) {
          fread(curr, sizeof(FLUG), 1, fp);
          printf("\n%d", curr-&gt;flugnummer);   // &lt;&lt;&lt;
          printf("\n%s", curr-&gt;flugziel);
          //fluege_sortieren(curr);
          PAUSE;
      }
  
  }
  
  int fluege_sichern() {
  ELEM_ZGR curr;
  FILE *fp;
  
  curr = first;
  
  if (fopen_s(&amp;fp, datei,"a+b") != 0) {
      printf("\nDatei %s nicht zum Anhaengen zu oeffnen",datei);
      PAUSE;
      exit(1);
  }
  
  for(curr = first; curr != NULL; curr = curr-&gt;next) {
      fwrite(curr, sizeof(FLUG), 1, fp);
  }
  
      fclose(fp);
      return 1;
  }
  </code></pre>