0

I'm stuck with the debugging the below code. I keep getting way too many jpg files written and a segmentation fault. Thanks in advance for the help. I'm new to programming and this one is kicking my butt.


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

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

    // Ensure proper usage, only one command line argument
    if (argc != 2)
    {
        fprintf(stderr, "Usage: ./recover image\n");
        return 1;
    }

    // Open memory card file
    FILE *inptr = fopen(argv[1], "r");

    if (inptr == NULL)
    {
        fprintf(stderr, "Could not open %s.\n", argv[1]);
        return 2;
    }

    // Create arrays for buffer and file name
    unsigned char buffer[512];
    char jpegname[8];

    // Create output file and output file counter
    FILE *jpeg = NULL;
    int jpegcount = 0;

    // Set bool to false
    bool jpegfound = 0;

    // Read file; check to see if at not EOF
    while (fread(buffer, 512, 1, inptr) == 1)
    {
        // Check for beginning of jpeg reading first four bytes using bitwise on last byte
        if(buffer[0]== 0xff && buffer [1] == 0xd8 && buffer [2] == 0xff && (buffer [3] & 0xf0) == 0xe0)
        {
            if (jpegfound == 1)
            {
                fclose(jpeg);
            }
            else
                jpegfound = 1;
        }

        sprintf(jpegname, "%03i.jpg", jpegcount);
        jpeg = fopen(jpegname, "w");
        jpegcount++;

        if (jpegfound == 1)
        {
            fwrite(&buffer, 512, 1, jpeg);
        }
    }
    fclose(inptr);
    fclose(jpeg);

    return 0;
}
1

Couldn't duplicate the seg fault, but I believe it's a side effect of the other issue.

So let's look at the code that creates files:

if(buffer[0]== 0xff && buffer [1] == 0xd8 && buffer [2] == 0xff && (buffer [3] & 0xf0) == 0xe0)
    {
        if (jpegfound == 1)
        {
            fclose(jpeg);
        }
        else
            jpegfound = 1;
    }

    sprintf(jpegname, "%03i.jpg", jpegcount);
    jpeg = fopen(jpegname, "w");
    jpegcount++;

The code is, for the most part, exactly right. But think about your analysis and this code. The program is creating thousands of files. It should be producing a lot less, and only when a signature is found. That's a really big clue.

The code checks the signature, and then, manipulates jpegfound. Then, it closes the code block that should only be processed when a signature is found. The next line of code, well, the next 3 lines, will execute for EVERY 512 byte block of data that is read. So, for every block of data read, a new file is created.

Isn't it amazing how much damage can be done by placing just one curly brace in the wrong place??? ;-)

Fix this, and if the seg fault still exists, it should be easy to identify and resolve, but I think it's just a side effect of the file creation interacting with the IDE.

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

  • Thank you! I was checking everything except the curly braces! Learned a valuable lesson. – JimL34 Apr 8 '19 at 4:29

You must log in to answer this question.

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