CS50 PSET4 RECOVER - Unable to recover 001.jpg and file 0049.jpg recovered does not match

一个人想着一个人 提交于 2021-01-29 09:05:20

问题


I'm attempting the 'Recover' question on CS50's pset4.

It appears that I am able to retrieve all of the images except for 001.jpg. I suspect that this further led to some misalignment with the numbering of the files given that check50 tells me the following:-

:) recovers 000.jpg correctly

:( recovers middle images correctly 001.jpg not found

:( recovers 049.jpg correctly recovered image does not match

To be clear, I was able to retrieve 000.jpg, followed immediately by files 002.jpg through 0050.jpg. The counter to keep track of the number of jpeg file seems to be working so I am very puzzled as to why 001 could possibly be skipped.

Would appreciate if someone could help me identify if there's any logical errors with my code. I have a hunch that some problem arises when the program jumps immediately to the 'else' branch where the very first 4 bytes are not JPEG headers? Thank you all very much in advance!

Here's my code:

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

typedef uint8_t BYTE;

int main(int argc, char *argv[])
{
  //Check for 1 command line argument
  if (argc != 2)
  {
    printf("Usage: ./recover image\n");
    return 1;
   }

//Opening forensic image file & getting its file pointer called "raw"
FILE *raw = fopen(argv[1], "r");

if (raw == NULL)        //Checking if pointer returns a null value
{
    return 1;
}

//Create counter to keep track of no. of JPEG files
int counter = 0;

//Creating a buffer with a size of 512 x BYTES
BYTE buffer[512];

//Creating a string for file name
char filename[8];

while (fread(buffer, sizeof(BYTE), 512, raw) == 512)
{

    //Checking if first 4 bytes are JPEG headers
    if (buffer[0] == 0xff &&
        buffer[1] == 0xd8 &&
        buffer[2] == 0xff &&
        (buffer[3] & 0xf0) == 0xe0)
    {
        //If it is first JPEG file
        if (counter == 0)
        {
            //Determine file name
            sprintf(filename, "%03i.jpg", counter);

            //Open new file
            FILE *output = fopen(filename, "w");

            //Write to new file
            fwrite(buffer, sizeof(BYTE), 512, output);

            //Increment JPEG file count by 1
            counter++;

            //Close file
            fclose(output);
        }
        else    //if not first JPEG file (i.e. counter !=0)
        {
            //Increment JPEG count first so that file name will be +1 as well
            counter++;

            //Determine next file name
            sprintf(filename, "%03i.jpg", counter);

            //Open new file
            FILE *output = fopen(filename, "w");

            //Write to new file
            fwrite(buffer, sizeof(BYTE), 512, output);

            //Close file
            fclose(output);
        }
    }
    else    //if first 4 bytes are not JPEG headers
    {
        //Open previous file (because there is no count increment)
        FILE *output = fopen(filename, "a");    //used 'a' instead to amend instead of 
                                                  overwriting previous file with 'w'

        //Write to previous file
        fwrite(buffer, sizeof(BYTE), 512, output);

        //close file
        fclose(output);
    }

    //For checking - to remove later
    printf("%i\n", counter);

}
fclose(raw);
return 0;

}


回答1:


 FILE *output = fopen(filename, "a");    //used 'a' instead to amend instead of 
                                                  overwriting previous file with 'w'

filename is not initialized.



来源:https://stackoverflow.com/questions/62328469/cs50-pset4-recover-unable-to-recover-001-jpg-and-file-0049-jpg-recovered-does

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!