Basically title. I'm getting a segmentation fault instead of 50 jpeg images when I run my program.
I've tried playing around with different conditions.
Code:
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
const int BLOCK_SIZE = 512;
FILE *img = NULL;
int main(int argc, char *argv[])
{
    if (argc != 2) // Ensure correct usage of program
    {
        printf("Usage: ./recover IMAGE");
        return 1;
    }
    FILE *card = fopen(argv[1], "r");
    if (card == NULL) // Check if file was successfully opened
    {
        printf("Error! File cannot be opened");
        return 1;
    }
    typedef uint8_t block; // type for buffer
    block store[BLOCK_SIZE]; // Buffer for storing data from card.raw
    int count = 0; // Counter for file name
    char filename[8]; // Array to store filename
    while (fread(store, 1, BLOCK_SIZE, card) == BLOCK_SIZE)
    {
        if (store[0] == 0xff && store[1] == 0xd8 && store[2] == 0xff && ((store[3] & 0xf0) == 0xe0)) // If start of a new jpeg
        {
            if (count == 0)
            {
                count++;
                sprintf(filename, "%03i.jpg", count); // Write filename with sequential number
                img = fopen(filename, "w"); // Open img file to write to
                fwrite(store, 1, BLOCK_SIZE, img);
            }
            else if (count > 0)
            {
                fclose(img);
                count++;
                sprintf(filename, "%03i.jpg", count);
                img = fopen(filename, "w");
                fwrite(store, 1, BLOCK_SIZE, img);
            }
        }
        else
        {
            fwrite(store, 1, BLOCK_SIZE, img);
        }
    }
}
Edit: Sorry, messed up formatting the first time around. Code should be in now. Also, I'm now getting output that I can open, but the images all consist of a gray and white checkerboard pattern.
 
    