I'm taking baby steps towards writing the code for recover. So far I have written this:
int count = 0;
int blocks = 0;
// Loops through the whole card.
while (feof(card_in) == 0)
{
// declares an array called buffer.
BYTE buffer[512];
// reads 512 bytes into buffer.
fread(&buffer, 512, 1, card_in);
// Checks if buffer[] contains the start of a JPEG.
for (int i = 0; i < 509; i++)
{
if (buffer[i] == 0xff &&
buffer[i+1] == 0xd8 &&
buffer[i+2] == 0xff &&
(buffer[i+3] & 0xf0) == 0xe0)
{
count += 1;
}
}
blocks += 1;
}
fclose(card_in);
printf("blocks read: %i\n", blocks);
printf("Found %i jpegs.\n", count);
}
Each time 512 bytes are read, I declare the array buffer, I'm wondering what happens to it when the loop reiterates. Does the code reallocate a new memory chunck somewhere else, so that the previous array still is stored somewhere, taking up memory? If that's the case, I guess I should somehow use malloc and clear, but I am unsure how to do this with an array. Do you have other comments on this bit of code, related to best practice? The code runs just fine, and counts 50 JPEGs...