#include <stdio.h>
    #include <stdint.h>
    #include <stdlib.h>
    #define FAT_BLOCK_SIZE 512
    // I figured out how to get block size from here "https://cs50.stackexchange.com/questions/28357/pset-4-recover-help-with-reading-512-byte-blocks"
    int main(int argc,char *argv[])
    // buffer_ptr[0] == 0xff && buffer_ptr[1] == 0xd8 && buffer_ptr[2] == 0xff && (buffer_ptr[3] & 0xf0)==0xe0) while(fread(&buffer_ptr,512,1,infile)
        // ensures correct number of arguments
        if(argc != 2)
            return 1;
            fprintf(stderr,"This function only accepts one file argument, the memory card");
        // open memory card file
        FILE *infile = fopen(argv[1],"r");
        if(infile == NULL)
            return 2;
            fprintf(stderr,"Failed to open file");
        //creates buffer
        uint8_t *buffer_ptr = malloc(FAT_BLOCK_SIZE);
        //initializes variables
        int jpg_bool = 1;
        int num_of_jpgs = 0;
        char filename[8];
        FILE *img = NULL;
        // while file can be read
        while(fread(buffer_ptr,1,512,infile) == 512)
            if(buffer_ptr[0] == 0xff && buffer_ptr[1] == 0xd8 && buffer_ptr[2] == 0xff && (buffer_ptr[3] & 0xf0)==0xe0)
                // Closes file before opening a new one
                if(img != NULL)
                // open a new jpg
                img = fopen(filename,"w");
                // write block
                // This tells the program that there is a found file and it is time to start writing
                jpg_bool = 0;
                // Increments name
            else if(jpg_bool == 0)
                // writes 512 bytes until new jpg found


I don't know why the images are wrong. I tried mapping out my algorithm but I couldn't find any faults. Algorithm

Check that are 2 Arguments

Open Memory Card

Create Buffer

Initialize Variables

While file can be read

if signature is at start of block

check for old file and close it

open new file

write block

make it clear that a file has been found

increment name

else if file already found

write block

1 Answer 1


Effectively your algorithm is correct, but there is a small mess with the pointers, you declare a pointer to the buffer in which it will be written:

uint8_t *buffer_ptr = malloc(FAT_BLOCK_SIZE);

So, what is buffer_ptr ?, a pointer, the name itself is the pointer, since we have thus declared this variable. And what is & buffer_ptr ?, well it is a pointer, since it is a memory address (that is a pointer, a variable that stores a memory address), but it is not the pointer that you think, but it is a memory address of the pointer itself, not the memory address where it points (the memory address where we write and we have reserved memory). What a mess!. Surprisingly you use this pointer correctly and incorrectly, the correct thing is:


By the way it does not free malloc, with free(), nor that close open files. There is some other small error, but I leave it to you so you can guess

  • Wow, after fixing that it spits out all 50 photos correctly. Thank you so much. I guess I really need to brush up on pointers if I let such a stupid error slip by.
    – Ryan
    Dec 29, 2018 at 12:50
  • No stupid mistakes, I still forget the semicolon after years of using c. Traditionally, pointers are considered a difficult topic. Simply say that you have to use them very carefully.
    – MARS
    Dec 29, 2018 at 13:30
  • I've been reading many of the threads and from what it looks like, the moderators would like to keep code that is 99% compiled and working off the forum in order to avoid honor code violations. Your code is almost complete, and with the minor correction made by @MARS, it solves the problem. You might want to hide the portions of the code not addressed in MARS's suggestion now that he solved it for you. Just a thought. Jan 30, 2019 at 20:23

You must log in to answer this question.

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