1

I was wondering if anyone could give me some guidance on this problem. I have the below code. When it runs, I see 000.jpg to 049.jpg, however all the pictures are black boxes? I'm not quite sure what's causing this? I would assume it's within fwrite?

while(fread(bytes, 512, 1, file)==1)
{

    if (bytes[0] == 0xff && bytes[1] == 0xd8 && bytes[2] == 0xff)
    {
        if(bytes[3] == 0xe0 || bytes[3]== 0xe1 || bytes[3]==0xe2 || bytes[3]==0xe3 || bytes[3]==0xe4 || bytes[3]==0xe5 || bytes[3]==0xe6 || bytes[3]==0xe7 || bytes[3]==0xe8 || bytes[3]==0xe9 || bytes[3] == 0xea || bytes[3]==0xeb || bytes[3]==0xec || bytes[3]==0xed || bytes[3]==0xee || bytes[3]==0xef)
        {



            if(jpeg == 0)
            {
                sprintf(filename, "000.jpg");
                img = fopen(filename, "a");


            }
            else if (jpeg<10 && jpeg >0)
            {
                fclose(img);
                sprintf(newfilename, "00%d.jpg", jpeg);
                img = fopen(newfilename, "a");
            }
            else
            {
                fclose(img);
                sprintf(newfilename, "0%d.jpg", jpeg);
                img = fopen(newfilename, "a");

            }


            fwrite(bytes, 512, 1, img);
            jpeg++;


        }
    }
    if (jpeg>=1)
    {
        fwrite(bytes, 512, 1, img);
    }

}
fclose(file);

fclose(img);
free(ptr);

}

2

Figured it out. I had to put the

if (jpeg>=1) { fwrite(bytes, 512, 1, img); } into an else, so that it would only execute if it wasn't the beginning of the file.

1
  • Thank you for this! I had exactly the same problem and was losing my mind! Dec 13 '18 at 12:16
0

Change first while(fread(bytes, 512, 1, file)==1) to this while(fread(&bytes, 512, 1, file)==1) and the same with fwrite(bytes, 512, 1, img) => fwrite(&bytes, 512, 1, img)

Look also how Your code check if conditions, compare to code from PSET4 Recover Problem to repair .jpg's signature in hexadecimal, it's a good example to make conditions shorter.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .