thanks for the replies, i have incorporated both suggestions, the result i get is now 37 jpgs (file does contain 37 even if i said 30 in the description in 1st post so that's correct) that are unreadable i also get the code below printed.
my question: im still not quite sure how to go about debugging the reason why the jpgs aren't being shown properly
Code:
found the start of a JPG file
jpg chunk: ff d8 ff e0
wrote name: 1.jpgfound the start of a JPG file
jpg chunk: ff d8 ff e0
wrote name: 2.jpgfound the start of a JPG file
jpg chunk: ff d8 ff e0
wrote name: 3.jpgfound the start of a JPG file
jpg chunk: ff d8 ff e0
wrote name: 4.jpgfound the start of a JPG file
jpg chunk: ff d8 ff e0
wrote name: 5.jpgfound the start of a JPG file
jpg chunk: ff d8 ff e0
...
...
wrote name: 36.jpgfound the start of a JPG file
jpg chunk: ff d8 ff e0
this is the updated code
Code:
/*
* ========================================================
* Description: Recovers pictures from card.raw which
* contains **37** jpgs in a row, then is blanked out with 0s.
* ========================================================
*/
#include <stdio.h>
#include <stdlib.h>
#include "bmp.h"
int
main(void)
{
// open card.raw
FILE *card = fopen("card.raw" , "r");
if (card == NULL)
{
printf("Could not open card.raw");
return 1;
}
// jpg number for output name
int jpg_number = 1;
// look at every byte of card.raw
BYTE jpg_chunk[512];
while (fread(&jpg_chunk, sizeof(BYTE), 512, card) != 0) // read current 512 byte
{
// test if that current 512 bytes is start of jpg
if (jpg_chunk[0] == 0xff && jpg_chunk[1] == 0xd8 && jpg_chunk[2] == 0xff &&
(jpg_chunk[3] == 0xe0 || jpg_chunk[3] == 0xe1))
{
// determine filename
char jpg_name[10];
sprintf(jpg_name, "%d.jpg" , jpg_number);
// create jpg
FILE *jpg_write = fopen(jpg_name, "w");
if (jpg_write == NULL)
{
fclose(jpg_write);
fprintf(stderr, "Could not create %s.\n", jpg_name);
return 2;
}
// TESTING
printf("found the start of a JPG file\n");
printf("jpg chunk: %x %x %x %x\n" , jpg_chunk[0], jpg_chunk[1], jpg_chunk[2], jpg_chunk[3]);
printf("wrote name: %s" , jpg_name);
do
// write to jpg
fwrite(&jpg_chunk, sizeof(BYTE), 512, jpg_write);
while (jpg_chunk[0] != 0xff && jpg_chunk[1] != 0xd8 && jpg_chunk[2] != 0xff &&
(jpg_chunk[3] != 0xe0 || jpg_chunk[3] != 0xe1));
// close finished jpg
fclose(jpg_write);
// this jpg is done, next jpg
jpg_number++;
}
}
// close card.raw
fclose(card);
// end
return 0;
}