0

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);

}    >
0

feof test whether a previous read operation has hit the end of the file. You would have to use it after the read operation, not before.

An alternative would be to use like while (fread(my_buffer, 512, 1, infile) == 1), which combines both functions.

FILE *img; does not initialise the variable, it might contain any content, especially it's not necessarily NULL.

On finding a new JPEG header, the only difference between img being NULL or not is that if it is not NULL, you'd want to use fclose to close the previous output file. In both cases, do all the sprintf and fopen and so on.

In any case, if you have an open output file, write the block to it. From the first JPEG header you encounter, every block should end up in exactly one output file.

The fseek does not make any sense to me. fread already consumed that block, so why would you skip another?

Also, final fclose(img); should be used only if there is a valid file structure pointer in img (i.e. you have actually opened an output file).

| improve this answer | |
  • thank you Blauelf for pointing out my mistakes. i made the corrections but i still get segmentation fault pastebin.com/smnvgD4m and i cant figure out where am going wrong – TabithaWamucii Aug 24 '17 at 12:29
  • Most likely this part of my answer: In any case, if you have an open output file, write the block to it. Instead of else { fwrite, try if (img != NULL) { fwrite. Also, you haven't done anything about the feof and the last fclose(img). – Blauelf Aug 24 '17 at 12:34
  • Also, (my_buffer[3]>=0xe0 || my_buffer[3]<=0xef) requires && instead of ||. Missed that on the original post. – Blauelf Aug 24 '17 at 12:38
  • I changed FILE *img; to FILE *img=NULL;, the while loop and others but i still get segmentation fault.i just cant figure out what is wrong.pastebin.com/iLFuLKTA – TabithaWamucii Aug 31 '17 at 9:11
  • The structure I'd expect in the loop could be like if (buffer[0]...) { if (img != NULL) { fclose(img); } /* here sprintf/fopen/counter++ */ } if (img != NULL) { fwrite(my_buffer, 512, 1, img); }. There's no reason not to open a new file on finding a file header. – Blauelf Aug 31 '17 at 11:18

You must log in to answer this question.

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