0

I know i've been asking a lot about this pset, but I assure I have done my research. My program for recover creates all of the files, but when I try to open them it says the file format is unsupported. I have printed some of the data on the files, the header is just like it's supposed to be. For the rest of the file, it shouldn't have any errors, because it was just copyed. Here's my code:

    FILE* in = fopen("card.raw", "r");
FILE* op;
char* temp = malloc(8);

int count = -1;

fread(buffer, 512, 1, in);
//keep reading untill there's no data
while(fread(buffer, 512, 1, in) !=0){
   if(buffer[0] == 0xff && buffer[1] == 0xd8 && buffer[2] == 0xff){
        if(buffer[3] == 0xe0 || buffer[3] == 0xe1){
            //if the start of a jpeg is detected, allocate memory for it's name

                free(temp);
                 temp = malloc(8);


            count +=1;
        }
    }
    if(count >=0){
        //writing the file's name and opening it
        sprintf(temp, "%03d.jpg", count);
        op = fopen(temp, "a");
        //copying the data in the buffer
        fwrite(buffer, 512, 1, op);
    }


}
//assuring temp is free
free(temp);

Can anyone spot the mistake?

Thank you very much.

1 Answer 1

0

I see several things that I am suspicious of. Keep in mind that this is based on only the code posted. Your remaining code could affect these observations.

First, while mallocing and freeing is a technically correct way to create the file pointer, it seems like overkill. You could create a single string array variable and reuse it as needed.

I see where the code opens new output files using the same pointer, but I don't see where the old output file is ever closed. Failing to close one file before opening a new file with the same pointer will cause problems.

The structure of the code is flawed. The if() that checks for the signature only frees and reallocates temp and increments count. Once the first signature is found and count is incremented, then EVERY block of code that follows will cause a file to be opened and the 512 byte block will be written to it. This is because the second if() is outside of the if() code that tests for the signature. THe first file will have a correct header, but the next files will not.

Because of the first fread before the while loop, the first block is simply thrown away. If there were a signature in it, the first file would be lost.

The code checks for two signatures. Contrary to the video (recycled from last year and a slightly different version of the assignment), the current assignment calls for 16 different signatures - see the pset instructions. (I wish they'd fix the video.)

There are a lot of issues going on, so there may be other issues, but this should get you going.

If this answers your question, please click ont he check mark to accept. Let's keep up on forum maintenance. ;-)

1
  • Once again, thank you. May I ask, are you a part of cs50's staff? Because most questions I see get answered not long after they've been asked, a lot of times by you. Leave the easier questions for us, clueless studentes, so we can contribute and not only leech from here :P Commented Mar 18, 2016 at 21:46

You must log in to answer this question.

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