First time on the site, I've been trying to do this pset by myself but as I write this thread, I have already spent easily more than 10 hours only in this code. I have reviewed it thoroughly once and again, and can't tell where my error is.
I suspect that it has to do with fread and fwrite quantity or position of them. Anyway, here's my code, please your help! Thanks in advance.
int main(void)
{
FILE* inptr = fopen("card.raw", "r");
if(inptr == NULL)
{
printf("File couldn't be opened\n");
return 1;
}
FILE* img = NULL;
uint8_t buffer[512];
uint8_t buf[512];
int counter = 0;
int Signature = 0;
char filename[8];
while(!feof(inptr))
{
Signature = 0;
if(fread(&buf, 512, 1, inptr) != 1)
{
break;
}
fread(&buffer, 512, 1, inptr);
if((buffer[0] == 0xff) && (buffer[1] == 0xd8) && (buffer[2] == 0xff) && (buffer[3] == 0xe0 || buffer[3] == 0xe1))
{
if (img != NULL)
{
fclose(img);
}
sprintf(filename, "%03d.jpg", counter);
img = fopen(filename, "w");
Signature = 1;
counter++;
fwrite(&buffer, 512, 1, img);
//fread(&buffer, 512, 1, inptr);
//printf("%u",buffer[0]);
}
if(counter != 0 && Signature == 0)
{
fwrite(&buffer, 512, 1, img);
}
}
if(img != NULL)
{
fclose(img);
}
fclose(inptr);
return 0;
}