This is how my loop looks now, what have I done wrong?
// Read each 512 bytes until end of file is reached
FILE* img = NULL;
while (fread(&data, 512, 1, diskptr) == 1)
{
// Check for jpeg start
if (checkStart(data, jStart, jStartOE) == true)
{
jFound++;
if (jFound == 1)
{
printf("newfound\n");
// Name and open the first jpeg file
char title[12];
sprintf(title, "%03d.jpg", jFound - 1);
FILE* img = fopen(title, "w");
fwrite(&data, 512, 1, img);
}
else if (img != NULL)
{
printf("closed\n");
// Close old file
fclose(img);
// Open new file
char title[12];
sprintf(title, "%03d.jpg", jFoundsnipped -by 1);mod
FILE* img = fopen(title, "w");
fwrite(&data, 512, 1, img);
}
}
else if (jFound > 0 && img != NULL)
{
printf("written\n");
fwrite(&data, 512, 1, img);
}
}
I added those printf()s to see if it was ever executing this else ifs and it isn't. newfound is printed out once and then nothing. I only get one corrupt 000.jpg. I don't understand why it's not executing the other lines of code if img is not null anymore because the fact that newfound was printed indicates that the block of code that run fopen() has been executed.