0

I've been stuck for days with this problem. My recover program creates 50 jpgs but none of them show.

When I look at the first 4 bytes of each, bytes 2 - 4 are correct for JPG signature.

But byte 1 is always 0 instead of 0xff. I don't understand how this could be happening. What am I missing??? Thank you for suggestions!

int main(void)
 {
//open raw data file 
FILE* rawfile = fopen("card.raw", "r");

 // buffer to hold 512 bytes from fread 
    typedef uint8_t  BYTE;
    BYTE buffer[512]; 
    char title[7];
    FILE* img = NULL;
    int search = 0; 
    int picnumber = 0; 

  while (feof (rawfile) == 0)
  {
      fread(&buffer, 512, 1, rawfile);

   //check for next jpeg signature 


       if (buffer[0] == 0xff && buffer [1] == 0xd8 && buffer [2] == 0xff && (buffer [3] >> 4) == 0xe)
          {


          //close current
            if ( search > 0)
            {
             fclose(img);
            } 
            //open new JPG and how to display file name 
              sprintf(title, "%03d.jpg", picnumber); 
              img = fopen(title, "a" );

             //write from buffer to new jpg, 
            fwrite(&buffer, 512, 1, img);
            picnumber++;
            search = 1;
          }
      else if (search > 0)
         {  
        fwrite(&buffer, 512, 1, img);   
         }
  }

}

1
  • Heads up to 2015 and earlier students: The card.raw file now contains 50 images. In prior years, it only had 16. Surprise! :-) But you should write your program to recover any number of images.
    – Cliff B
    Commented Jan 9, 2016 at 22:19

1 Answer 1

5

The problem is happening because the first byte of your buffer is being overwritten. The data that is being inserted is the end of string marker '\0'. It's being written there because buffer is located just after another string variable in memory. That other string variable is title[].

Why is this happening? You allocated a length of 7 chars to title. Each file name that you are creating is 7 characters long, like 000.jpg. So, when the code writes the filename out to title, where is it going to put the end of string marker '\0'? Normally, it would go into the 8th position, but title only has 7.

If this answers your question, please click on the check mark to accept. Let's keep up on forum maintenance. ;-)

5
  • That was it! I changed title to 8 chars and all the pics showed up! Thank you!!! This is so cool. Now I remember the lecture about strings having the \0 at end. Could you explain the memory arrangement a bit more? If I missed this in the video lectures and walk throughs, sorry for asking you to repeat it. From your explanation, would my program work with title being 7 chars if buffer was located BEFORE title in memory? I know its not good practice and it should be 8. If so, is it just the order that I declare my variables in my program that stacked title Before buffer? Thank you
    – Xia
    Commented Jan 8, 2016 at 19:04
  • Nevermind...I just tested it and it caused a set fault.
    – Xia
    Commented Jan 8, 2016 at 19:09
  • The order isn't the problem, it was just a symptom. The problem was that not enough space was allocated to title. Whatever followed title in memory was going to have the first byte overwritten.
    – Cliff B
    Commented Jan 8, 2016 at 19:15
  • I had the same problem as the original poster. Thank you for the solution!
    – Eli
    Commented Mar 26, 2017 at 7:33
  • thank you times a million!!
    – 8moonbowl
    Commented Oct 30, 2020 at 21:12

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .