0
#include <stdio.h>

#include <stdlib.h>


//int main(int argc, char* argv[])

int main(void)
{    // TODO
    int count=0;
    char name[8];

    unsigned char buffer[512]={0};
    FILE * outptr=NULL;
    FILE* inptr=fopen("card.raw","r");
    if(inptr==NULL)
    {
        printf("nao foi possivel abrir o arquivo");
        return 1;
    }
    while(fread(buffer , sizeof(buffer) , 1 ,inptr))
    {
              // fread(buffer , sizeof(buffer) , 1 ,inptr);
         fwrite(buffer, sizeof(buffer) , 1, outptr);
        if(((buffer[0]==0xff) && (buffer[1]=0xd8) && 
(buffer[2]==0xff) && (buffer[3]==0xe0)) || (buffer[3]==0xe1)
        || (buffer[3]==0xe2) || (buffer[3]==0xe3) || (
buffer[3]==0xe4) || (buffer[3]==0xe5) || (buffer[3]==0xe6) 
        || (buffer[3]==0xe7) || (buffer[3]==0xe8) || 
(buffer[3]==0xe9)|| (buffer[3]==0xea)|| (buffer[3]==0xeb) || 
        (buffer[3]==0xec) || (buffer[3]==0xed) || 
(buffer[3]==0xee) || (buffer[3]==0xef))

        {
            if(outptr!=NULL)
            {
                fclose(outptr);
               // outptr=NULL;
            }
            sprintf(name, "%03d.jpg",count);
          //count++;
            outptr=fopen(name, "a");// outptr=fopen(jpg_name,w);
            fwrite(buffer, sizeof(buffer) , 1, outptr);
            count++;
        }
               else  if(outptr != NULL)
           { 
               fwrite(buffer, sizeof(buffer) , 1, outptr);
           }
             }

   fclose(outptr);

    fclose(inptr);

    return 0;
}
1

Like in maths * has higher precedence than +, && has a higher precedence than ||. That means you'd have to put parentheses around your expressions combined with ||.

But you could avoid those 16 terms completely by applying a bitmask. You want to check whether the fourth byte's upper nibble is 0xe, the lower nibble doesn't matter, as all values from 0xe0 to 0xef are valid. You can write (buffer[3] & 0xf0 == 0xe0), with 0xf0 being the bitmask. Binary AND & results in a number having binary ones where both input values have a one, and zeroes where any of them has a zero, so this operation keeps only the bits where 0xf0 has ones, and that's the higher-value nibble. For example you might have the value 0xe2.

0xe2        11100010
0xf0        11110000
0xe2 & 0xf0 11100000 (0xe0)

As another option, maybe less elegant than & but still more readable than your original approach, you could write (buffer[3] >= 0xe0) && (buffer[3] <= 0xef)

You must log in to answer this question.

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