All I'm getting is segmentation fault

include <stdio.h>

include <stdlib.h>

include <cs50.h>

bool detect_sig(unsigned char buffer[]); void jpeg_read(char* card); char* make_name(int jpeg_count);

int main(int argc, char *argv[]) { int jpeg_count = 1;

//check command line argument for storage
if (argc != 2)
{
    printf("usage: ./recover image");
}

//asign storage as variable card
char* card = argv[1];

//function that does all the work
jpeg_read(card);

}

//check header to detect signature though the first indices bool detect_sig(unsigned char buffer[]) { if (buffer[0] == 0xff) { if (buffer[1] == 0xd8) { if (buffer[2] == 0xff) { if ((buffer[3] & 0xf0) == 0xe0) { return true; } } } } return false; }

//main function void jpeg_read(char* card) { //create a pointer as the FILE structure to the open file FILE *card_f = fopen(card,"r");

//create unsigned chars that can be compared as hexidecimals as a buffer to load data in
unsigned char buffer[512];

// designate temporary filename for some reason?
char* filename = "temp";

//designate while loop check early
int size_t = 512;

//counter to know if a file has been written yet
bool first = true;

//designate counter to enumerate a name for jpeg files
int jpeg_count = 1;

//initialize pointer for where you're writing
FILE *img;

//while fread returns 512 keep running
while (size_t == 512)
{
    //load buffer with block of data and return number of characters that was loaded
    size_t = fread(buffer,1,512,card_f);

    //if signature of loaded buffer passes check then continue
    if (detect_sig(buffer))
    {
        //if first time writing
        if (first)
        {
            //use function to make enumerating name as filename
            filename = make_name(jpeg_count);
            //enumerate name counter
            jpeg_count++;
            //create empty file with created name
            img = fopen(filename,"w");
            //write the buffer into the file
            fwrite(buffer,1,512,img);
            //remember that you have written a first file
            first = false;
        }
        else
        {
            //close the last file
            fclose(img);
            //make a new filename
            filename = make_name(jpeg_count);
            //enumerate name counter
            jpeg_count++;
            //open a new file to write in with new name
            img = fopen(filename,"w");
            //write buffer into file
            fwrite(buffer,1,512,img);

        }
    }
    //if not the start of a new jpeg then continue writng blocks to file
    else
    {
        fwrite(buffer,1,512,img);
    }
}
free(filename);

} //make an enumerating file name char* make_name(int jpeg_count) { //give space for the name to be written char* filename = malloc(8); //create the name with jpeg counter as the last value sprintf(filename, "%03i.jpg",jpeg_count); //return the name for the outside function return filename; }

/r/cs50 Thread