【问题标题】:CS50 PSET4 RECOVER - Unable to recover 001.jpg and file 0049.jpg recovered does not matchCS50 PSET4 RECOVER - 无法恢复 001.jpg 和文件 0049.jpg 恢复不匹配
【发布时间】:2020-06-11 15:59:25
【问题描述】:

我正在尝试 CS50 的 pset4 上的“恢复”问题。

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

:) 正确恢复 000.jpg

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

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

明确地说,我能够检索到 000.jpg,紧随其后的是文件 002.jpg 到 0050.jpg。跟踪 jpeg 文件数量的计数器似乎正在工作,所以我很困惑为什么可能会跳过 001。

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

这是我的代码:

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

}

【问题讨论】:

    标签: pointers cs50 recovery


    【解决方案1】:
     FILE *output = fopen(filename, "a");    //used 'a' instead to amend instead of 
                                                      overwriting previous file with 'w'
    

    filename 未初始化。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-07-22
      • 1970-01-01
      • 2021-05-11
      • 1970-01-01
      • 2020-08-02
      • 2020-08-08
      相关资源
      最近更新 更多