0

My code recover 12 files. first one is opening, rest are not. What is wrong with the code?

#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>

typedef uint8_t  BYTE;

int main(int argc, char* argv[])
{
// Open the memory card file
FILE* card = fopen("card.raw", "r");

if (card == NULL)
{
    printf("could not open the file. \n");
    return 1;
}


FILE* img = NULL;
//array of bytes
BYTE ptr[512];

//image number
int count = 0;

//size of the file
while(fread(ptr, 512, 1, card) == 1)
{
    if ((ptr[0] == 255 && ptr[1] == 216 && ptr[2] == 255 && ptr[3] == 224) ||(ptr[0] == 255 && ptr[1] == 216 && ptr[2] == 255 && ptr[3] == 224))
    {
        char title[10];
        sprintf(title, "%03d.jpg", count);

        //increase image number
        count++;

        //create new image file
        img = fopen(title, "w");

        if(img == NULL)
        {
        printf("error\n");
        return 1;
        }

        do
        {
            //write into the new file
            fwrite(ptr, 512, 1, img);
            fread(ptr,512,1,card);


        }
        while ((ptr[0] != 255 && ptr[1] != 216 && ptr[2] != 255 && ptr[3] != 224) ||(ptr[0] != 255 && ptr[1] != 216 && ptr[2] != 255 && ptr[3] != 224));

       //because once fread has been executed
        fseek(card, -512, SEEK_CUR);

    }
}

fclose(card);
//close created image
        fclose(img);
return 0;
}
1

I think it's because the condition in your do-while loop. Look at this Answer of a similar Question, and specifically the third item in the list, about De Morgan's laws.

Q: Developing JPEGS for pset4's recover troubleshooting.

The condition inside your do-while loop is probably wrong. The opposite of

(ptr[0] == 255 && ptr[1] == 216 && ptr[2] == 255 && ptr[3] == 224) || (ptr[0] == 255 && ptr[1] == 216 && ptr[2] == 255 && ptr[3] == 224)

is

(ptr[0] != 255 || ptr[1] != 216 || ptr[2] != 255 || ptr[3] != 224) && (ptr[0] != 255 || ptr[1] != 216 || ptr[2] != 255 || ptr[3] != 224)

and not

(ptr[0] != 255 && ptr[1] != 216 && ptr[2] != 255 && ptr[3] != 224) || (ptr[0] != 255 && ptr[1] != 216 && ptr[2] != 255 && ptr[3] != 224)

If you want to use a lazy way, and not learn about De Morgan's laws, you can just negate the original condition using the ! operator. So the opposite of

(ptr[0] == 255 && ptr[1] == 216 && ptr[2] == 255 && ptr[3] == 224) || (ptr[0] == 255 && ptr[1] == 216 && ptr[2] == 255 && ptr[3] == 224)

is

!((ptr[0] == 255 && ptr[1] == 216 && ptr[2] == 255 && ptr[3] == 224) || (ptr[0] == 255 && ptr[1] == 216 && ptr[2] == 255 && ptr[3] == 224))

If this answers your question please accept it by clicking the gray check-mark to the left, so that it becomes green. You can also vote it up by pressing the up arrow above the check-mark.

EDIT

You also have a naive mistake, which is probably because of a typo. You use two conditions to check for ptr, because the 4th byte in buffer ptr can take 2 possible values: 0xe0 == 224 or 0xe1 == 225. On both occasions you check for 0xe0 == 224. That's why you don't find all the pictures.

Sidenote

You would be better off using the hexadecimal values, as given in the specification, so use ptr[0] == 0xff and nor ptr[0] == 255, which is the same, but the former is easier to debug and follow. Also you can shorten your condition, as the 3 first bytes are the same in both cases, and only the fourth changes, but I will leave that to you.

4
  • yeah I got it. But do you think everything else is correct? Because I still get only 12 pictures and 2 of them open, rest don't. Aug 21 '15 at 21:47
  • Check the edits.
    – ChrisG
    Aug 21 '15 at 21:58
  • 1
    Thanks my friend..Still there are little errors but I will sort it. Aug 21 '15 at 22:06
  • Don't forget that if this answers your question you can accept it by clicking the gray check-mark to the left, so that it becomes green. You can also vote it up by pressing the up arrow above the check-mark.
    – ChrisG
    Aug 21 '15 at 22:09

You must log in to answer this question.

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