0

#include <stdio.h> #include <stdlib.h> #define BYTE 512 #define NAME 8

int main(int argc, char *argv[]) { //check for valid input if(argc != 2) { //Promt user for valid input printf("invalid input.\nCorrect input; ./recover filename\n"); return 1; }

//remember file  name.
char *Infile = argv[1];

//Make file pointer for memory card and img file.
FILE *Infileptr = fopen(Infile, "a");

//Make sure that the provided file pointer isn't null.
if(Infileptr == NULL)
{
    return 1;
}

//Make buffer
unsigned char Buffer[BYTE];

//Make string for sprintf
char recovered[NAME];

//Make file pointer for images
FILE *outFileptr = NULL;

//Make counter for the number of JPEGs found to name the files accordingly
int name = 0;

//Bool to check if a new JPEG is found.
int already_found = 0;


//Read the file and Check for EOF
while(fread(Buffer, BYTE, 1, Infileptr) == 1)
    {
        //Look for JPEG
        if(Buffer[0] == 0xff && Buffer[1] == 0xd8 && Buffer[2] == 0xff && (Buffer[3] & 0xf0) == 0xe0)
        {
            //For an opened JPEG when a new JEPG is found.
            if(already_found == 1)
            {
                fclose(outFileptr);
            }
            
            //For the first JPEG.
            else
            {
                already_found = 1;
            }

            sprintf(recovered, "%3i.jpg", name);
            name++;
            outFileptr = fopen(recovered, "w");

        }
        
        //For an opened JPEG when there's still no new JPEG
        if(already_found == 1)
        {
            fwrite(Buffer, BYTE, 1, outFileptr);
        }
    }
if(outFileptr == NULL)
{
    fclose(outFileptr);
}

if(Infileptr == NULL)
{
    fclose(Infileptr);
}
return 0;

}

  • Can you please add context to this? Which line of code is causing the seg fault? What have you figured out about the error? What do you think is the problem? This post is called a naked code drop - posting of code with little or no explanation and asking others to find and solve the problem, and is against forum rules. Please read the following: cs50.stackexchange.com/questions/37065/… – Cliff B Oct 5 at 18:27
  • Yes! extreamly sorryy – user29898 Oct 6 at 2:51

You must log in to answer this question.

Browse other questions tagged .