I am not familiar on how to post my question on this forum so little help on that will be appreciated.i have tried to tweak my code i either get invalid image or segmentation fault. so i post the one i think its a bit logical but i get segmentation fault. i will appreciate your advice. > https://pastebin.com/tgxUNK5k <
Blockquote
# include <stdio.h>
# include <stdlib.h>
int main(int argc,char *argv[])
{
if(argc!=2)
{
fprintf(stderr,"argc shld b 1\n");
return 1;
}
FILE *infile=fopen(argv[1],"r");
if(infile==NULL)
{
fprintf(stderr,"can not open file\n");
return 2;
}
//temporaly storage
unsigned char my_buffer[512];
// char array to store the resultant string
char *file_name=malloc(sizeof(char)*512);
int count=0;
FILE *img;
//to iterate over infile
for(int block_count=0;!feof(infile);block_count++)
{
//read the card.raw block by block
fread(&my_buffer,512,1,infile);
//check if block start with jpeg
if(my_buffer[0]==0xff && my_buffer[1]==0xd8 && my_buffer[2]==0xff && (my_buffer[3]>=0xe0 || my_buffer[3]<=0xef) )
// you have found a jpeg
{
if(img==NULL)
{
sprintf(file_name,"%03i.jpg",count);
img=fopen(file_name,"w");
//write image to outfile till end
fwrite(&my_buffer,512,1,img);
count++;
}
else
{
fwrite(&my_buffer,512,1,img);
}
}
else
fseek(infile,512,SEEK_CUR);
}
fclose(img);
fclose(infile);
free(file_name);
} >