I'm getting a segmentation fault when trying to run my program for pset4 recover. I've been trying for ages to debug it, but I'm finding it really difficult because when I try to use debug50, after the first line of the while loop (while (fread(buffer, 1, sizeof(buffer), card))", it simply skips the whole loop and goes to the bottom of the program.

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

typedef uint8_t BYTE;
char filename[8];

int main (int argc, char* argv[])
    // Check command-line arguments
    if (argc != 2)
        printf("Usage: ./recover image\n");
        return 1;
    //Open card
    FILE *card = fopen("card.raw", "rb");
    //Initialize output file
    FILE *jpeg;
    //Create buffer
    BYTE buffer [512];
    //Initialize counter
    int i = 0;
    //Start reading contents of card
    while (fread(buffer, 1, sizeof(buffer), card))
        //Check for the start of the first jpeg
        if ((buffer[0] == 0xff) && (buffer[1] == 0xd8) && (buffer[2] == 0xff) && ((buffer[3] & 0xf0) == 0xe0) && i == 0)
            //Create first jpeg
            sprintf(filename, "%03i.jpg", i);
            jpeg = fopen(filename, "wb");
            //Start writing contents of card into first jpeg file and increment counter
            fwrite(buffer, 1, sizeof(buffer), jpeg);
        //Check for start of subsequent jpeg files
        else if ((buffer[0] == 0xff) && (buffer[1] == 0xd8) && (buffer[2] == 0xff) && ((buffer[3] & 0xf0) == 0xe0) && i > 0)
            //Close previous file
            //Create new file
            sprintf(filename, "%03i.jpg", i);
            FILE *out = fopen(filename, "wb");
            //Start writing contents of card into new jpeg file
            fwrite(buffer, 1, sizeof(buffer), jpeg);
        //If file is larger than 512B, continue adding blocks to the file until new jpeg is found
        else if (i > 0)
            fwrite(buffer, 1, sizeof(buffer),jpeg);
    //When end of the card is reached, close final jpeg and close card


enter image description here

2 Answers 2


I couldn't duplicate any seg fault. Did you remember to recompile after editing your code?

The code is almost working. Unfortunately, it only creates two output files. Maybe you should check on how file names are being created and variables incremented? ;-)

Also, if card.raw is empty, it will cause seg faults later in the code when the fclose calls are attempted at the end. No files were opened.

You might benefit from this: Do YOU know how to find a seg fault?? Advice to new programmers

BTW, why is the name of the input file hardcoded instead of using the input parameter when the program is run?

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

  • How strange, I'm still getting Segmentation fault when I run it. Tried recompiling just to be on the safe side, and got the same result. Any ideas why this could be?
    – BlueKhakis
    Feb 16, 2021 at 2:12
  • Are you using the online IDE or is your IDE local on your computer? Also, are you sure that you're recompiling the correct source code file? Can you try to recompile, take a screen shot and add it to the question above (edit the question to add)?
    – Cliff B
    Feb 16, 2021 at 2:13
  • Sure, what should I take a screenshot of?
    – BlueKhakis
    Feb 16, 2021 at 2:18
  • The entire IDE window
    – Cliff B
    Feb 16, 2021 at 2:18
  • Edited. To clarify, I'm using the CS50 IDE.
    – BlueKhakis
    Feb 16, 2021 at 2:29

You never verify that jpeg was opened, so if there is no opened file, your line fclose(jpeg); will cause a segfault.

Be sure to always check a file is open before trying to close it.

You must log in to answer this question.

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