0

I seem to have a segmentation fault with my recover code. I also tried check50 and it said that an error had occurred. Any tips or help would be appreciated!

// Copies a BMP file

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


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

// remember filenames
char *infile = argv[1];

// open input file
FILE *inputr = fopen(infile, "r");
if (inputr == NULL)
{
    fprintf(stderr, "Could not open %s.\n", infile);
    return 2;
}
unsigned char buffer[512];
char *filename = NULL;
int counter = 0;
FILE *img = NULL;

while (sizeof(buffer) == 512)
{
    fread(buffer, 1, 512, inputr);
        printf("WHILE loop works\n");
    if (buffer[0] == 0xff && buffer[1] == 0xd8 &&
    buffer[2] == 0xff && (buffer[3] & 0xf0)== 0xe0) {
        printf("IF statement works\n");
        if (counter == 0){ //first jpg
            sprintf(filename, "%03i.jpg", counter);
            img = fopen(filename, "w");
            fwrite(buffer, 1, 512, img);
            fclose(img);
            counter++;
        }
        else if (counter > 0){
            sprintf(filename, "%03i.jpg", counter);
            img = fopen(filename, "w");
            fwrite(buffer, 1, 512, img);
            fclose(img);
            counter++;
        }

    }
    else {
        fwrite(buffer, 1, 512, img);
    }

}




// close file
fclose(inputr);

// success
return 0;

}

~Updated Code~

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

// remember filenames
char *infile = argv[1];

// open input file
FILE *inputr = fopen(infile, "r");
if (inputr == NULL)
{
    fprintf(stderr, "Could not open %s.\n", infile);
    return 2;
}
unsigned char buffer[512];
char *filename = NULL;
int counter = 0;
FILE *img = NULL;

while (sizeof(buffer) == 512)
{
    fread(buffer, 1, 512, inputr);
        printf("WHILE loop works\n");
    if (buffer[0] == 0xff && buffer[1] == 0xd8 &&
    buffer[2] == 0xff && (buffer[3] & 0xf0)== 0xe0) {
        printf("IF statement works\n");
        if (counter == 0){ // first jpg
            printf("Writing first jpg\n");
            sprintf(filename, "%03i.jpg", counter);
            img = fopen(filename, "w");
            fwrite(buffer, 1, 512, img);
            counter++;
        }
        else if (counter > 0){
            printf("Writing next jpg\n");
            fclose(img);
            sprintf(filename, "%03i.jpg", counter);
            img = fopen(filename, "w");
            fwrite(buffer, 1, 512, img);
            counter++;
        }

    }
    else if (counter > 0) { // continue writing only after file is opened
        printf("Continuing jpg\n");
        fwrite(buffer, 1, 512, img);
    }

}




// close file
fclose(inputr);

// success
return 0;

}

0

You were on the trail to finding the problem, but stopped short with the print statements. ;-)

There are several issues in the code, but we'll stick to the seg fault. Think about what's happening. The first block is read in. It's garbage data, so the code skips down to the else clause and tries to write the data out. Since this is the first block, no output file has been opened. The fwrite tries to write to a null file, causing the seg fault. Sounds like an if test is needed to see if the file has been opened.

Here's a bonus. Once that's fixed, think about the declaration for filename. It's a pointer. Maybe a char array would work better?

There are other issues, but you need a chance to find them and work on them. If needed, please open a new question for them.

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

1
  • Thank you, your answer was very helpful. I feel like I have fixed quite a few of my logic and code errors (and I've provided the updated code above). However, I still have a segmentation fault. My code starts to write the first jpg but it faces a fault right after that. I guess my first question should be what exactly is a segmentation fault? I feel like if I fully understand that it would greatly help in how I handle the problem. Thanks again! – Tenacity Apr 19 '18 at 18:04

You must log in to answer this question.

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