0

I've checked and reread my code a million times but I can't understand why I'm always getting a segmentation fault. I don't even know where the segmentation fault is. Any help is greatly appreciated!

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

char* inname = argv[1];
FILE *file = fopen(inname, "r");

if (file == NULL)
{
    fprintf(stderr, "The forensic image cannot be opened for reading.\n");
    return 2;
}

unsigned char buffer[512];
int contador = 0;
char* filename = "";
FILE *outptr = NULL;

while (fread(buffer, 512, 1, file) == 1)
{
    // check if its the start of a new jpeg file
    if (buffer[0] == 0xff && buffer[1] == 0xd8 && buffer[2] == 0xff && (buffer[3] & 0xf0) == 0xe0)
    {
        if (outptr == NULL)
        {
            sprintf(filename, "%03i.jpg", contador);
            contador++;
            outptr = fopen(filename, "w");
            fwrite(buffer, 512, 1, outptr);
        }
        else // if a jpef was already open
        {
            // close the opened jpeg
            fclose(outptr);

            // open a new jpeg
            sprintf(filename, "%03i.jpg", contador);
            contador++;
            outptr = fopen(filename, "w");
            fwrite(buffer, 512, 1, outptr);
        }
    }
    else
    {
        if (outptr != NULL)
        {
            fwrite(buffer, 512, 1, outptr);
        }
    }
}

fclose(file);
fclose(outptr);

return 0;
}
3
  • It would be easy to tell you what is causing the seg fault, but what would you learn if I did? I'd really like to see you narrow it down to one specific line of code. Debugging skills are very critical to any programmer. Part of that is knowing how to locate an errant line of code. You need to first isolate the code that is creating the seg fault. You can either run the code through debug (which sometimes won't produce obvious results for a segfault) or you can add some temporary printf statements to the code to narrow down the location to a single line. – Cliff B Sep 25 '17 at 5:33
  • The problem is that debug 50 was not helpful at all, I never got to the error but instead it just exits the while loop like it has nothing else to read. I'll try to find the error with some printing to the screen. – Santiago Donado Torres Sep 25 '17 at 11:56
  • I made it! Error was here I assigned the space for filename... If I give it "", then how will I assign anything to that? Silly error, but now its fixed! – Santiago Donado Torres Sep 25 '17 at 12:15
1

Answered in comments / Glad you figured out how to find the problem! If your question is answered, please click on the check mark to accept and remove this question from the unanswered question pool. Let's keep up on forum maintenance. ;-)

It would be easy to tell you what is causing the seg fault, but what would you learn if I did? I'd really like to see you narrow it down to one specific line of code. Debugging skills are very critical to any programmer. Part of that is knowing how to locate an errant line of code. You need to first isolate the code that is creating the seg fault. You can either run the code through debug (which sometimes won't produce obvious results for a segfault) or you can add some temporary printf statements to the code to narrow down the location to a single line. – Cliff B 14 hours ago

The problem is that debug 50 was not helpful at all, I never got to the error but instead it just exits the while loop like it has nothing else to read. I'll try to find the error with some printing to the screen. – Santiago Donado Torres 8 hours ago

I made it! Error was here I assigned the space for filename... If I give it "", then how will I assign anything to that? Silly error, but now its fixed! – Santiago Donado Torres 7 hours ago

You must log in to answer this question.

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