'Ve been working on recover for days. It finally compiled but not sure what's happening coz it ain't creating any intended file (from "%03d.jpg") and I can't open the file (it gets automatically deleted after popping error message "could not open file"). Below is an image of the funny file the program created:

enter image description here

and here is my code so far

#include <stdio.h>



typedef uint8_t BYTE;

typedef struct { BYTE block; } JPEG;

int main(void) {
// declare variables int file_num = 0; char title[8]; uint8_t data[512]; JPEG img;

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

// open new file
FILE* new_file = fopen(title, "a");

// measure card.raw size
long long card = sizeof("card.raw");

// put stream at the beginning of file 
fseek(file, 0, SEEK_SET);

// to repeat until end of card
for(int i = 0, n = card / 512; i <= n; i++)
    // read 512 bytes into a buffer
    fread(&data, 512, 1, file);

    // start of a new jpg?
    // check for jpg signature
    if(data[0] == 0xff && 
       data[1] == 0xd8 && 
       data[2] == 0xff &&
       (data[3] >= 0xe0 && data[3] <= 0xef))
        // yes, then write 512 bytes to new file
        sprintf(title, "%03d.jpg", file_num);

        // write file data
        fwrite(&img, 512, 1, new_file);
        // already found a jpg?
        // yes, then ...(keep track and write into JPEG) 
        if(file_num > 0)
            // continue writing jpg data
            fwrite(&img, 512, 1, new_file);
        // no, then don't write at all or skip
            // move stream to the next block
            fseek(file, (i + 1) * 512, SEEK_CUR);

    // eof??
    if(fread(&data, card, 1, file) == 0)
        printf("%i\n files have been recovered!!", file_num);

// close any remaining files
// close source file

// close output file

// that's all folks
return 0;


Could anyone please help give me a hint where this program goes wrong? Thanking you beforehand.

1 Answer 1


From the spec (sanity checks):

Does recover output 50 JPEGs? Are all 50 viewable?

One can assume, then, that the program will create 50 different files. How many files does this program create? There are two fopen in the function, one for card.raw (reading) and one, new_file, for writing. It creates exactly one file. And what is the name of new_file? The value of title. And what is the value of title when the fopen is executed? It hasn't been initialized, so it's apparently the "funny files".

Once you get that sorted, be advised that "a" is not the best choice for the fopen argument. Since you want to create a "new" file each time, "w" is the generally accepted choice. There may be other problems, and you will need to work them through once you get a chance to evaluate the files created.

  • Thanks for the tips, Dino. I'll look into this and 'll get back to you once I've figure it out (fingers cross). :-)
    – MTreacy
    Oct 24, 2016 at 22:01
  • Just fixed the problem (in relation to opening new files). Now I make it so the new file open and close once it's done before opening another jpeg file. I've also rearranged sprintf and open file codes. have now moved a step ahead with my program able to print 1 jpeg file (even though failed to get a clear picture). That'll be posted in different question. Thanks again Dino for the tips.
    – MTreacy
    Oct 27, 2016 at 4:02
  • The hint in the pset about using a while loop to read in the data 512 at a time is a good one. You might consider using it.
    – Zach
    Mar 14, 2023 at 23:05

You must log in to answer this question.

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