title: Binary file reading/writing in C
body: <p>So I have an input.bin file which contains the following</p>
  
  <pre><code>IK-SZH;jdl72u;John Doe;2013-03-28 11:05
  IK-GRR;kat91n;Jane Doe;2013-03-21 15:41
  IK-GRR;oat62f;Jane Doe;2013-03-24 08:08
  </code></pre>
  
  <p>What I am doing is to read it into a struct. Doing some stuff with the data. Add/Delete lines.
  Then i would like to write the content of the structure back to the input.bin file in the same format as above.</p>
  
  <p>But instead of appearing as it is above. It's like this (no spaces):</p>
  
  <pre><code>IK-SZH NUL jdl72u NUL John Doe NUL NUL NUL NUL NUL 2013-03-28 NUL NUL NUL IK-GRR NUL kat91n NUL Jane Doe NUL NUL NUL NUL ...
  </code></pre>
  
  <p>When I re-read the file (with the NULs) It only puts the 1st line into the struct</p>
  
  <p>My code</p>
  
  <pre><code>typedef struct foo {
      char kid[7];    
      char jid[7];    
      char name[21];  
      char time[20];  
  
  } Foo;
  
  Foo foo[200];
  FILE* fp;
  int size;
  </code></pre>
  
  <p>------- File reader</p>
  
  <pre><code>void read(char* filename){
      fp = fopen(filename, "rb"); 
      int i = 0;
  
      while (!feof(fp)) {
  
          if (fp==NULL){perror("File opening error\n"); exit(1);}
  
          fscanf(fp,"%[^;]; %[^;]; %20[^\;]; %s\n", foo[i].kid, foo[i].jid,
                  foo[i].name, foo[i].time);
          i++;
      }
  
      size = i;
  
      print();
  
      fclose(fp);
  }
  
  
  void print(){
      int i;
      for (i = 0; i &lt; size; ++i){
          printf("%s\t %s\t %s\t %s\n", foo[i].kid, foo[i].jid,
                 foo[i].name, foo[i].time);
      }
  }
  </code></pre>
  
  <p>----- Writer</p>
  
  <pre><code>void write(){
      char str[1000];
  
      FILE* f = fopen("input.bin", "wb");
      fseek(f, 0, SEEK_SET);
      int i;
      for (i = 0; i &lt; jel_size; i++)
              fwrite(&amp;foo[i], sizeof(struct foo), 1, f);
      fclose(f);
  
  }
  </code></pre>
  
  <p>Tried this, but this didn't write anything to the file:</p>
  
  <pre><code>    char str[1000];
      sprintf(str,"%s;%s;%s;%s\n", jelent[i].kazon,
                      jelent[i].jazon,jelent[i].nev,  jelent[i].ido );
  
          write(f,&amp;str,sizeof(str))!=sizeof(str);
  </code></pre>