title: Reading\Writing Binary File with char's and float's
body: <p>I am pretty new to C and like the tittle says I am trying to write a simple program to read and write files in binary. The code is as follows:</p>
  
  <pre><code>#include&lt;stdio.h&gt;
  
  int main(void){
  
      FILE *fd = fopen("binFile.bin", "wb");
  
      if(fd == NULL){
          printf("Failed to open/create file.\n");
          return -1;
      }
  
      char buff1[] = "#This is a comment.\n";
      fwrite(buff1,1,sizeof(buff1),fd);
      char buff2[] = "#This is another comment.\n";
      fwrite(buff2,1,sizeof(buff2),fd);
  
      int i;
      float f[3];
  
      for(i=0; i&lt;100; i++){
          f[0] = i-1;
          f[1] = i;
          f[2] = i+1;
  
          fwrite(f,sizeof(f),1,fd);
      }
  
      fclose(fd);
  
      fd = fopen("binFile.bin", "rb");
  
      if(fd == NULL){
          printf("Failed to read file.\n");
          return -1;
      }
  
      char buff[100];
  
      do{
          fread(buff,1,sizeof(buff),fd);
          printf("%s",buff);
      }
      while(!feof(fd));
  
      fclose(fd);
  
      return 0;
  }
  </code></pre>
  
  <p>When I run this code it only prints:</p>
  
  <pre><code>#This is a comment.
  </code></pre>
  
  <p>I know I am not using a bunch of checks for the file; however, I think the problem is that I am trying to read char's and float's with the same buffer since using the same code just for char's (or just float's) works just fine. I am guessing I have to somehow know where the bytes of char's end and the float's begin to adjust my buffer size/type accordingly. </p>
  
  <p>I hope I explain myself sufficiently. Any help is appreciated. </p>