In this weeks post I will be going over how to implement Recover from CS50 Problem Set 4. When we start looking at the code below, by now the first few lines should look very familiar. These are items that you have implemented in several other programs throughout the course. We are simply making sure that the user has supplied us with one command line argument, we then open that file to read, and throw an error if that file is null. The following is the actual implementation of how to recover the images that were deleted.

The first thing here is to create a pointer to a file that is null. This file is where we will eventually store all our recovered images.  Next, we create two arrays. One that will house the 512 bytes for every image and one to house that images file name. Remember, that we must name each file in increments starting at 000. To do this, we next set a counter that will count up as each image is recreated.  Next, we set a boolean flag and initialize it to false. We will change this flag to true later on to indicate to our program when we have reached the end of an image.

To house the rest of the code, we start with a while statement. The statement says that it will continue to loop through the file while each chunk is equal to 512 bytes. Remember that programs return 1 when they are successful. Which is why we set the statement equal to 1. The next statement is the if statement to make sure we are reading a JPEG. Remember that eventually we will reach empty space on the drive, so we need a way to determine if the 512 bytes we are reading is truly a JPEG. The condition for this if statement is given through the walkthrough for this exercise. Watch the video of the walkthrough for the explanation of this condition statement.

The next if statement will close the image if we have reached the end of the image. Remember, we never want to leave files open after we are done with them.  Then if we have found a JPEG the flag is set to true. The next statements prints the filename, opens a new image file with that filename, and then increases the counter by 1. By increasing the counter, we are able to name our files in the necessary way for this exercise. Finally we write the 512 byte buffer array to the image file and recover the image. Once the loop has finished executing we make sure that we close all our open files and return 1 to are program indicating successful completion.

And viola! You have now recovered all of the deleted images from the drive. If this post was helpful to you, please spread the word by sharing on your favorite social media platform. Also, be sure to comment below with any questions. As always…Happy Programming!


  1. Hi, would you mind explaining why there are 8 elements in the array filename of char?
    Thanks for your reply, it would help me alot.


    1. Yes, the filename is an array of chars. Now, our code must generate a filename that is three digits (3 chars), followed by a period (1 char), followed by jpg (3 chars). The last index is for the null terminator character.


  2. Hi, doesn’t C programs return 0 when successful and 1 when unsuccessful? If so, how does ‘while (fread(buffer, 512, 1, file) == 1)’ work?


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s