I spent hours but still then i'm not able to understand that why does check50 does not recognize my 49th recovered image eventhough i have recovered that image and i can see that the image is of Jamyla here is my code

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

  typedef uint8_t BYTE;

int main(void)

// open card.raw file
FILE *inptr = fopen ("card.raw", "r");
if (inptr == NULL)
    printf("could not open card.raw file\n");
    return 1;

// size of a block in FAT file system 
int block_size = 512;

// buffer store one block
BYTE buffer[block_size];

// store image filename 
char image_name[8];

// recovered jpgs will be named with a number starting from 1  
int number = 0;

// keep track of when an image is found 
bool image_found = false;

// pointer to current jpeg file on hard disk
FILE *outptr = NULL;

// look for jpgs until EOF is reached 
while (feof(inptr) == false)
    // read 512 btye block into buffer
    fread(buffer, block_size, 1, inptr);

    // check buffer for jpeg signature
    if (buffer[0] == 255 && buffer[1] == 216 && buffer[2] == 255 &&
        (buffer[3] == 224 || buffer[3] == 225))
        // if a previous jpeg was found close file 
        if (image_found == true)

        image_found = true;

        // name new image
        sprintf(image_name, "%.3d.jpg", number);

        // create new jpg image file on hard disk
        outptr = fopen (image_name, "w");
        if (outptr == NULL)
            printf("could not create jpeg file\n");
            return 2;

        // write 512 byte block to current jpeg file 
        fwrite(buffer, block_size, 1, outptr);        

    // continue writing parts of a jpg to file on disk 
    else if (image_found == true)
        fwrite(buffer, block_size, 1, outptr);        

// close card.raw 

// close last jpg
    return 0;

someone please guide me what to do.As this is the only part of pset5 that i'm stuck on.


feof(inptr) won't get set to false until fread tries to read past the end of the file. So, the last time through your loop, what happens? fread will set the feof flag but, since you are already in the loop, the loop will continue running, and you'll end up writing the last block twice. Check50 will not expect this and will return a :(

  • I know that curiouskiwi but I'm not able to understand how to rectify that. Could you please tell me how to do it. Dec 22 '14 at 7:45

Like what Zamyla mentioned in the videos, you can use the fread function in a condition and it will still run that function. Therefore you can turn your first fread call into a condition that will work so long as there are blocks left.

You must log in to answer this question.

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