0

I am not able to open the images properly, and here is my code

typedef uint8_t  BYTE;
BYTE buffer[512];
char title[10];
int count =0;
FILE* fp = fopen("card.raw","r");
while(!feof(fp))
{
     fseek(fp, sizeof(buffer), SEEK_CUR);
     fread(&buffer,32,1,fp);
    while (!(buffer[0] == 0xff && buffer[1] == 0xd8 && buffer[2] == 0xff && (buffer[3] == 0xe0 ||buffer[3] == 0xe1)))
    {
      fread(&buffer,32,1,fp);  

    }
    sprintf(title, "%03d.jpg" ,count );
   FILE* image = fopen(title, "a");
     while (!(buffer[0] == 0xff && buffer[1] == 0xd8 && buffer[2] == 0xff && (buffer[3] == 0xe0 ||buffer[3] == 0xe1)))
    {   
    fwrite(&buffer, 512, 1, image);
    }
    count ++;
    fclose(image);

}
fclose(fp);

}

1 Answer 1

1
  1. while(!feof(fp)) Review Week 4 > Shorts > File I/O (starting at 7:30) to remember why you shouldn't use !feof to control the while loop.
  2. fread(&buffer,32,1,fp); 32? From the spec:

cameras only write to those cards in units of 512 B.

  1. (buffer[3] == 0xe0 ||buffer[3] == 0xe1)) Make sure you are working from the current specification. It says:

The fourth byte, meanwhile, is either 0xe0, 0xe1, 0xe2, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7, 0xe8, 0xe8, 0xe9, 0xea, 0xeb, 0xec, 0xed, 0xee, of 0xef.

  1. Beware the "a" here FILE* image = fopen(title, "a"); If any of the jpg files already exist, the data will be appended, and thus create a corrupt file. From man fopen:

w Truncate file to zero length or create text file for writing. The stream is positioned at the beginning of the file.

You must log in to answer this question.

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