How do I locate the source of a segmentation fault? (CS50: recover.c) -


i trying make program in c cs50 recovers jpg's .raw file (reads 512 bytes @ time , sees if begins jpg stuff), keeps segmentation faulting. how tell source of problem is? guys! (here's code reference)

      /**  * recover.c  *  * computer science 50  * problem set 4  *  * recovers jpegs forensic image.  */   //0xff 0xd8 0xff 0xe0  //0xff 0xd8 0xff 0xe1  #define block 512 #define start1end 0xe0 #define start2end 0xe1  #include <stdio.h> #include <cs50.h> #include <stdlib.h> #include <stdint.h>  //making variables int found = 0;  char* title; file* img; int ifopen = 1;  int main(int argc, char* argv[]) {     //opening file     file* inptr = fopen("card.raw", "r");     //checking if file opening failed     if (inptr == null)     {         return 2;     }     //sets begins or jpgs     uint8_t checkjpg1[4] = {0xff, 0xd8, 0xff, 0xe0};     uint8_t checkjpg2[4] = {0xff, 0xd8, 0xff, 0xe1};      //making buffer     unsigned char buffer[512];      //going through file     while(fread(&buffer,sizeof(char),block,inptr) == block)     {          //checking if begin == possible begin of jpg              if ((buffer[0] == checkjpg1[0] && buffer[1] == checkjpg1[1] && buffer[2] == checkjpg1[2]) &&           (buffer[3] == checkjpg1[3] || buffer[3] == checkjpg2[3]))          {             //if jpg not open             if (ifopen == 1)             {                 //make 1                 found+=1;                 sprintf(title,"00%d",found);                 img = fopen(title,"a");             }             else//else             {                 //end 1 , open new 1                 fclose(img);                 sprintf(title,"00%d",found);                 img = fopen(title,"a");             }          }          else if(img != null)          {              fwrite(buffer,sizeof(char),block,img);          }     }      fclose(inptr);     free(buffer); } 

(sorry long overflowing lines!)

in line (and others)

sprintf(title,"00%d",found); 

there no memory allocated title, declared

char *title; 

but all.

char title[block]; 

would better. incidentally, don't use block when declaring buffer should

unsigned char buffer[block]; 

also, need another

found+=1; 

before opening of img in else code block.


Comments

Popular posts from this blog

google chrome - Developer tools - How to inspect the elements which are added momentarily (by JQuery)? -

angularjs - Showing an empty as first option in select tag -

php - Cloud9 cloud IDE and CakePHP -