0

I am trying to complete recover.c for pset4, but I am having major trouble debugging. I get a segmentation fault after the first fwrite step has occured, and it creates the fault at the fread step at the start of the next iteration of the while loop.

The code is below

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

int main(int argc, char *argv[])
{
    // only accept single additional command line argument
    if (argc != 2)
    {
        fprintf(stderr, "Usage: ./recover image\n");
        return 1;
    }

    // save name of input file
    char *infile = argv[1];

    // open file to read in and make sure it opens correctly
    FILE *input = fopen(infile, "r");
    if (input == NULL)
    {
        fprintf(stderr, "Input file was not opened correctly\n");
        return 2;
    }

    int NoBytes = 0, piccount = 1;
    char jflag = '0';
    char *pic = "000.jpg";
    FILE *output = NULL;

    // create buffer for 512 bytes to be read into
    uint8_t *buff = malloc(512 * sizeof(uint8_t));

    // loop through entire card
    while (1)
    {
        // check for eof
        NoBytes = fread(buff, 1, 512, input);
        if (NoBytes != 512)
        {
            break;
        }

        // check to see if file is of type jpg
        if (buff[0] == 0xff &&
            buff[1] == 0xd8 &&
            buff[2] == 0xff &&
            (buff[3] & 0xf0) == 0xe0)
        {
            // condition when first jpeg discovered
            if (jflag == '0')
            {
                // open output file
                output = fopen(pic, "w");

                // check file is created
                if (output == NULL)
                {
                    fprintf(stderr, "Could not create output %i\n", piccount - 1);
                    return -1;
                }
            }
            else
            {
                // close previous file
                fclose(output);

                // create pic file
                sprintf(pic, "%03i.jpg", piccount);
                piccount++;

                output = fopen(pic, "w");

                // check file is created
                if (output == NULL)
                {
                    fprintf(stderr, "Could not create output %i\n", piccount - 1);
                    return -1;
                }
            }

            // set start of jpeg flag if not already set
            if (jflag == '0')
            {
                jflag = '1';
            }
        }

        // if jpeg has not been found yet
        if (jflag == '0')
        {
            continue;
        }
        else
        {
            fwrite(buff, 1, 512, output);
        }

    }
    fclose(input);
    fclose(output);
    free(buff);
    return 0;
}
0

char *pic = "000.jpg"; creates a string literal, stored in memory that cannot be changed. You will crash when you try to change that string with sprintf. If you need an empty array to hold your string name, you should declare it as an array long enough to hold "000.jpg" (plus the null char) like this: char pic[8];

You must log in to answer this question.

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