PLEASE I am dying: I am searching for hours for the problem, but I can't find it. It compiles. At least 000. and 001. should be found but I get these results and I am full of remorse.

Where could be the problem of my code?

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

//    andsoforth

// I guess the problem starts here    
// :((((((((((((((((((

    int jpg_counter = 0;
    FILE *output_file = NULL;
    unsigned char buffer[buffer_size];
    int jpg_found = 0;
    char filename[8];
    while (fread(buffer, buffer_size, 1, input_file) == 1 )
        (buffer[0] == 0xff &&
            buffer[1] == 0xd8 &&
            buffer[2] == 0xff &&
            (buffer[3] & 0xf0) == 0xe0)

            //If start of # new JPEG
            //return 1;
            if (jpg_found == 1)
            sprintf(filename, "%03i.jpg", jpg_counter);
            output_file = fopen(filename, "w");
           //count equals 2
            if (jpg_found == 0)
                fwrite(buffer, buffer_size, 1, output_file); 

    return 0;


1 Answer 1


Hang in there! I remember having my fair share of issues as I worked through this problem set.

Looking over your code, I think it might be worthwhile for you to read -- very carefully -- through Recover's background and specification. I find it super helpful to break down the larger problem into a bunch of little problems, and the specification provides a guide for doing just that.

For instance, the second bullet point in the specification states:

Your program should accept exactly one command-line argument, the name of a forensic image from which to recover JPEGs.

Right now, your program doesn't appear to handle any command-line arguments. Instead of stressing about the problem in its entirety, maybe you could focus on generating some code that accepts only one command-line argument. And then once you've solved that sub-problem, you can move on to another sub-problem.

It might be helpful to look at the last three pages of the source code for week 4's lecture. Also, there are plenty of hints on the Recover problem-set page. And the walk-through videos (also on the problem-set page) are really clear and super helpful if you're in the weeds.

** Also, one concrete hint: these sentences from the pset4 background section are useful when it comes to determining the size of the buffer, the middle two arguments of the fread function, and the program's logic in general:

"... digital cameras often initialize cards with a FAT file system whose “block size” is 512 bytes (B)."


"rather than read my memory card’s bytes one at a time, you can read 512 of them at a time into a buffer for efficiency’s sake. Thanks to FAT, you can trust that JPEGs’ signatures will be “block-aligned.” That is, you need only look for those signatures in a block’s first four bytes."

I hope you find this helpful! Looking forward to seeing what you come up with!

You must log in to answer this question.

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