title: cs50x pset4 recover: Recovers 50 JPEGS, but half or less of the image
body: <p>My program finds all 50 jpgs. However, the images are 'incomplete'. Some are half recovered, some less than a quarter, some are just completely grey. When I opened a jpeg, that appears grey when opened, in hexadecimal it shows that there are mostly colored pixels, which makes no sense. I've attempted to find the bug from all angles.</p>
  
  <p>Can anyone please help me understand why my images are 'corrupted'?</p>
  
  <pre><code>// 000.jpg
  int j = 0;
  // storing 000.jpg
  char jpeg[8];
  // buffer
  unsigned char bf[512];
  // FILE pointer
  FILE *img = NULL;
  // reading into memory card file
  while(fread(bf, 512, 1, inptr) == 1)
  {
      if (bf[0] == 0xff &amp;&amp; bf[1] == 0xd8 &amp;&amp; bf[2] == 0xff &amp;&amp; (bf[3] &amp; 0xf0) == 0xe0)
      {
          sprintf(jpeg, "%03i.jpg", j);
          img = fopen(jpeg, "w");
          do
          {
              fwrite(bf, 512, 1, img);
              fread(bf, 512, 1, inptr);
          }
          while(bf[0] != 0xff &amp;&amp; bf[1] != 0xd8 &amp;&amp; bf[2] != 0xff &amp;&amp; (bf[3] &amp; 0xf0) != 0xe0);
          fclose(img);
          j++;
          // fread is going to read it again
          fseek(inptr, -512, SEEK_CUR);
      }
  }
  fclose(inptr);
  return 0;
  }
  </code></pre>