I dont get why all of my jpeg's appear but they are all invalid. When i try to open them none of them opens and gives "invalid or unsupported Image format" error.Can someone help me to spot what am i doing wrong ? Here's my code :
#include <stdio.h>
#include <stdlib.h>
int main(void){
FILE* infile = fopen("card.raw","r");
if(infile == NULL){
printf("Could not load the memory card");
return 1;
}
unsigned char memory[512] = {0};
// array for filename is 8 for "000.jpg"
char jpgName[7];
int jpgcounter = 0;
FILE* outfile = NULL;
while(fread(memory,sizeof(memory),1,infile) == 1){
if(memory[0] == 0xff && memory[1] == 0xd8 && memory[2] == 0xff && (memory[3]>=0xe0 || memory[3]<=0xef)){
if(outfile != NULL){
fclose(outfile);
}
sprintf(jpgName,"%0.3d.jpg",jpgcounter);
outfile = fopen(jpgName,"a");
fwrite(memory,sizeof(memory),1,outfile);
jpgcounter++;
}
else{
if(outfile != NULL){
fwrite(memory,sizeof(memory),1,outfile);
}
}
}
fclose(infile);
fclose(outfile);
}