0
#include <stdlib.h>
#include <stdint.h>

//to use uint8_t as BYTE
typedef uint8_t BYTE;

int main(int argc, char *argv[])
{
    if (argc != 2)
    {
        printf("INVALID. There should be 2 arguments\n");
        return 1;
    }

    FILE* input = fopen("argv[1]", "r");
    if (input == NULL)
    {
        printf("INVALID. Input NULL pointer\n");
        fclose(input);
        return 1;
    }

    //creates an array of 512 bytes
    BYTE buffer[512];

    //File pointer to output
    FILE* output;

    //counts number of JPEG. Initially set to -1. Each time JPEG particular header is obtained it increments
    int count_jpeg = -1;

    /*array of 8 characters since filename will contain 8 character.
    example - 000.jpeg contains 7 characters but string ends with \0 so 1 extra charater means total character is 8 */
    char filename[8];

    while(fread(buffer, sizeof(BYTE), 512,input))
    {
        //checks if 1st 4 bytes of buffer is JPEG specific
       if(buffer[0] == 0xff && buffer[1] == 0xd8 && buffer[2] == 0xff && (buffer[3] & 0xf0) == 0xe0)

        {   //increments as new JPEG is found
            count_jpeg++;
            //close already opened output file
            if(count_jpeg != 0)
            {
                fclose(output);
            }
            // generates a string. like 000.jpg. this string is stored in filename(declared above)
            sprintf(filename, "%03i.jpg", count_jpeg);

            //opens an output file  in write mode with output file name as name of filename
            output = fopen(filename, "w");

            //closes file if output ponter is NULL
            if(output == NULL)
            {
                printf("INVALID, Output NULL POINTER\n");
                return 1;
            }

            //writes the 512 bytes from buffer to output
            fwrite(buffer,sizeof(BYTE), 512, output);
        }

        //continues to next 512 bytes which follow the JPEG
        else if(count_jpeg >= 0)
        {
            fwrite(buffer,sizeof(BYTE), 512, output);
        }
    }

//closes the input & outfile
fclose(input);
fclose(output);
}

Now when I run ./recover card.raw I get the output as INVALID. Input Null pointer & then in next line it prints Segmentation Fault.

Why is it giving Segmentation Fault?

1
  • Well, found the error. In FILE* input = fopen("argv[1]", "r");, argv[1] should not be inside quotations. But still why does writing those quotations give segmentation fault? Jun 6 at 8:59
1

When you put double quotes around argv[1], it says use this text as a literal file name, so the program goes looking for a file named argv[1]. Since the file doesn't exist, the fopen() call fails and the file pointer is set to NULL. Then, the code prints that error message.

The seg fault is caused by the next line, fclose(input). Since the input pointer is null, there's nothing to close, thus causing a seg fault.

If this answers your question, please click on the check mark to accept. Let's keep up on forum maintenance. ;-)

You must log in to answer this question.

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