0

I think I'm very close to getting this one right, but I've been trying different codes with the same results so I think my logic must be wrong. This code finds correctly the jpegs and creates the files; the first block line is represented correctly, but as it keeps writing the image start to get distorted until it doesn't make sense. I hope someone can shine some light on where my mistake might be.

int jpeg_count = 0;

int jpeg_header[4] = {0xff, 0xd8, 0xff, 0xe0};

char filename[8]; // 7 chars + null character = 8 xxx.jpeg

BYTE buffer[512];

FILE* new_imgp = NULL;

// while is able to read at least 512 bytes, if not stop reading as the end of the file has been reached

while (fread(buffer, sizeof(BYTE), 512, fp))

{

// read the first 4 bytes from a 512 bytes block (FAT)

// clear the last four bits so it always get a 0 on the las byte

buffer[3] = buffer[3] & 0xf0;

// jpeg found: create a new file and open a file for writting

if (check_jpeg(buffer, jpeg_header))

{

// if the filep contains something it means is not the first time a jpeg has bee found

// therefore, close the file before opening a new one

if (new_imgp != NULL)

{

fclose(new_imgp);

}

// create a new file and open it for writting

sprintf(filename, "%03i.jpg", jpeg_count);

new_imgp = fopen(filename, "w");

jpeg_count++;

}

if (new_imgp != NULL)

{

// write the 512 block found by find_jpeg

fwrite(buffer, sizeof(buffer), 1, new_imgp);

}

}

fclose(fp);

fclose(new_imgp);

return 0;

You must log in to answer this question.

Browse other questions tagged .