0

Really struggling with this one hope someone can help!

I'm getting a segmentation fault with this code and despite fiddling around with it, debugging and checking other answers on this site I can't for the life of me get it to work!

It simply returns segmentation fault without creating any JPEG files.

int main(int argc, char *argv[])
{

    if (argc != 2)
    {
        fprintf(stderr, "Usage: ./recover image\n");
        return 1;
    }



    //opens raw data file and checks this has been done correctly
    FILE *rawptr = fopen(argv[1], "r");
    if (rawptr == NULL)
    {
        fprintf(stderr, "Could not open %s.\n", argv[1]);
        return 2;
    }


    //Sets up variables and bools for use later
    int Buffer[512];
    bool inJPEG = false;
    bool endOfFile = false;
    int JPEGNumber = 0;
    char currentJPEG[8];
    FILE *img = NULL;
    int byteCount = 0;


    //Checks the rawfile, one block at a time

    while(endOfFile == false)
    {

    for (int i = 0; i < 512; i++)
    {
    fread(&Buffer[i], 512, 1, rawptr);
    byteCount++;
    }
    //Checks for end of file encountered and breaks if so
    if (byteCount < 512)
    {
        endOfFile = true;
        break;
    }
    byteCount = 0;
    //Checks for start of new JPEG
        if (Buffer[0] == 0xff && Buffer[1] == 0xd8 && Buffer[2] == 0xff && (Buffer[3] & 0xf0) == 0xe0)

        {
            inJPEG = true;
            //Checks if a JPEG has already been found
            if (!inJPEG) //starts the first JPEG
            {
                sprintf(currentJPEG, "%03i.jpg", JPEGNumber);

            img = fopen(currentJPEG, "a");
            JPEGNumber++;
            }

            else //Closes the last JPEG and opens a new one
            {
                fclose(img);
                sprintf(currentJPEG, "%03i.jpg", JPEGNumber);

            img = fopen(currentJPEG, "a");
            JPEGNumber++;
            }
        }

        if(inJPEG) //writes current block to JPEG if one is open
        {
            for (int i = 0; i < 512; i++)
            {
            fwrite(&Buffer[i], 512, 1, img);
            }
        }


    }

    //Once end of file has been encountered, closes active file and ends program
    fclose(img);
    fclose(rawptr);
    return 0;

}

Any hints in the right direction would be greatly appreciated!

0

Look at the following:

for (int i = 0; i < 512; i++)
{
fread(&Buffer[i], 512, 1, rawptr);
byteCount++;
}

The fread call will read 512 bytes. The for loop executes it 512 times. That's 512 * 512 bytes read, or about 256k. Just a guess, but I'd say that you're overwriting a lot of memory allocated elsewhere, eventually causing a seg fault.

There may be other issues, but this is the seg fault.

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

1
  • Thank you so much, that did indeed fix the segmentation fault, knew it would be some sort of daft mistake like that! – Chris6102 Nov 22 '17 at 20:58

You must log in to answer this question.

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