title: Binary 0000 to FFFF using C
body: <p>I am trying to program using C to write binary data to a .bin file and just iterate through to write from 0000 to FFFF. I figured I would use fopen with a 'wb' tag and then be able to write binary data but I'm unsure how to iterate from 0000 to FFFF using C. Thanks for any help.</p>
  
  <p>Here's my code now:</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  
  int main()
  {
   FILE *f = fopen("binary.bin", "wb");
   unsigned long i;
  
   //if(f == NULL) { ...error handling... }
  
   for(i = 0x0000; i &lt;= 0xFFFF; i++){
    // Write something to the file, e.g. the 16-bit (2 byte) value of "i"
    unsigned short someData = i;
    fwrite(&amp;someData, 1, 2, f);
   }
  
   fclose(f);
   return 0;
   //printf("Hello World\n");
   getchar();
  }
  </code></pre>
  
  <p>This will output 00 00 01 00 02 00 ...</p>
  
  <p>Here's my question now. Isn't this supposed to read out 00 00 00 01 00 02...Shouldn't there be an extra '00' at the beginning?</p>
  
  <p>Also, I've been trying to see how could I copy it and extend it therefore making it 0000 0000 0001 0001 etc?
  [Update: I just copied the fwrite line and did it again and it solved this problem]</p>