0

I've been checking lots of answers to people with a similar problem. But I don't get this to work yet. My code recovers all the images, but the last image is empty. I've read answers and check the video of recover and the thing is that if I use at the while loop the condition fread(&buffer, sizeof(BUFFER), 1, sdcard) == 1, only 25 images are created and non of them shows the correct picture, only distorsions with colors. If I don't use this condition, as my code is writen here, I recover the 50 images, being the last one the only one that shows incorrectly. I would like to ask if you could help me to achieve a solution for this problem. Thanks

// TODO BUFFER buffer; char title[8] = "";// Will hold the names of the jpegs files int i = 0;// Counter for the char array for jpegs //int x = 0;// Condition for while loop

// open input file 
FILE* sdcard = fopen("card.raw", "r");

if (sdcard == NULL)
{
    printf("Could not open card.raw\n");
    return 2;
}

FILE* jpg = NULL;

while(i != 50){
// read


fread(&buffer, sizeof(BUFFER), 1, sdcard);

if(buffer.block[0] == 0xff && buffer.block[1] == 0xd8 && buffer.block[2] == 0xff &&
(buffer.block[3] == 0xe0 || buffer.block[3] == 0xe1)){

if(jpg != NULL){
fclose(jpg);   
}   
sprintf(title, "%03d.jpg", i);
i++;

// open input file 
jpg = fopen(title, "w");

if (jpg == NULL)
{
    printf("Could not open jpg\n");
    return 1;
}
}
if(jpg != NULL){    
// write jpg
//int no_read = fread(&buffer, sizeof(BUFFER), 1, sdcard);

fwrite(&buffer, sizeof(BUFFER), 1, jpg);

}


}


fclose(jpg);

fclose(sdcard);


return 0;

}

0

I've solve it. I was using fread after the condition in the while loop, when that wasn't necessary.

I founded the answer here

pset4 recover I'm confused with fread

| improve this answer | |

You must log in to answer this question.

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