CS50 PSET4 RECOVER-无法恢复001.jpg,并且恢复的文件0049.jpg不匹配 [英] CS50 PSET4 RECOVER - Unable to recover 001.jpg and file 0049.jpg recovered does not match

查看:107
本文介绍了CS50 PSET4 RECOVER-无法恢复001.jpg,并且恢复的文件0049.jpg不匹配的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试对CS50的pset4进行恢复"问题.

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

看来,我能够检索除001.jpg之外的所有图像.我怀疑这进一步导致了文件编号的一些不一致,因为check50告诉我以下内容:-

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:-

:)正确恢复000.jpg

:) recovers 000.jpg correctly

:(正确恢复中间图像找不到001.jpg

:( recovers middle images correctly 001.jpg not found

:((可正确恢复049.jpg恢复的图片不匹配

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

为清楚起见,我能够检索000.jpg,然后立即检索文件002.jpg到0050.jpg.跟踪jpeg文件数量的计数器似乎正在工作,因此我很困惑为什么可能会跳过001.

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.

如果有人可以帮助我确定我的代码是否存在逻辑错误,将不胜感激.我有一种预感,当程序立即跳转到"else"分支(其中前4个字节不是JPEG标头)时,会出现一些问题?提前非常感谢大家!

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!

这是我的代码:

#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;

}

推荐答案

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

文件名未初始化.

这篇关于CS50 PSET4 RECOVER-无法恢复001.jpg,并且恢复的文件0049.jpg不匹配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆