0

I dont understand why my debugger stops working on this line of code

while(fread(buf1, sizeof(BYTE) * 512, 1, file) == 512 && buf1[0] != 0xff && buf1[1] != 0xd8 && buf1[2] != 0xff && (buf1[3] <= 224 || buf1[3] >= 239))

The code snippet of the part copying the jpg images is

while (1)
{
    if (fno < 10)
    {
        sprintf(title, "00%d.jpg", fno);
        FILE* outptr = fopen(title, "w");
        if (outptr == NULL)
        {
            printf("Could not open 00%d.jpg.\n",fno);
            return 2;
        }
        fwrite(buf1, sizeof(BYTE)*512, 1, outptr);
        while(fread(buf1, sizeof(BYTE) * 512, 1, file) == 512 && buf1[0] != 0xff && buf1[1] != 0xd8 
        && buf1[2] != 0xff && (buf1[3] <= 224 || buf1[3] >= 239))
        {
            fwrite(buf1, sizeof(BYTE)*512, 1, outptr);
        }
        fclose(outptr);
        fno++;
    }
    else
    {
        sprintf(title, "0%d.jpg", fno);
        FILE* outptr = fopen(title, "w");
        if (outptr == NULL)
        {
            printf("Could not open 0%d.jpg.\n",fno);
            return 2;
        }
        fwrite(buf1, sizeof(BYTE)*512, 1, outptr);
        while(buf1[0] != 0xff && buf1[1] != 0xd8 
        && buf1[2] != 0xff && (buf1[3] <= 224 || buf1[3] >= 239))
        {
            if (fread(buf1, sizeof(BYTE) * 512, 1, file) != 512)
            {
                flag3 = 1;
                break;
            }
            fwrite(buf1, sizeof(BYTE)*512, 1, outptr);
        }
        fclose(outptr);
        fno++;
    }
    if (flag3 == 1)
    {
        break;
    }
}
0

Not sure what you mean by 'stops working', but the while loop will never execute. The fread() call will return 1 for a successful read, not 512. Fread will return the number of elements read, not the size of the read. The second parameter in your call is the element size, 512 bytes in this case. The third parameter is the number of elements to be read, 1 in this case, so it will return 1 on a successful read.

If this answers your question, please click on the check mark to accept. Let's keep up on forum maintenance. ;-)

You must log in to answer this question.

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