My code for pset3 works and recovers all the images as it is supposed to however I realised that when I call fopen the first value in my buffer array becomes 0 I fixed this by changing the value back to 0xff right after I call fopen but I have no idea why fopen is doing this, I hope that someone could help me understand why exactly this is happening.
while (fread(buffer, 512, 1, inptr) != 0)
{
if ((buffer[0] == 0xff) && (buffer[1] == 0xd8) && (buffer[2] == 0xff) && ((buffer[3] & 0xf0) == 0xe0))
{
if (i > 0)
{
fclose(img);
}
sprintf(filename, "%03i.jpg", i);
img = fopen(filename, "w");
i++;
buffer[0] = 0xff;
}
fwrite(buffer, 512, 1, img);
}
(I didnt post all of the code since I am not sure if that goes against the academic honesty policy but I think this should be enough)
The if statement only triggers when buffer[0] == 0xff but somehow if I don't include buffer[0] = 0xff; after the call to fopen the value changes to 0 I know this from checking the hexadecimal for the files being made. Am I just missing something or is fopen really changing the values?