CS50 Pset4 恢复:正确恢复 049.jpg,恢复的图像不匹配

CS50 Pset4 Recover: recovers 049.jpg correctly, recovered image does not match

尝试提交时,CS50 pset4 recover.c然后出现错误

:) recover.c 存在。 :) recover.c 编译。 :) 处理缺少取证图像 :) 正确恢复 000.jpg :) 正确恢复中间图像 :( 正确恢复 049.jpg 恢复的图像不匹配

我的密码是

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

#define FAT 512

typedef uint8_t BYTE;

int main(int argc, char *argv[])
{
    if (argc != 2)
    {
        fprintf(stderr, "Usage: name of forensic image from which to recover JPEGs");
        return 1;
    }
    FILE *file = fopen(argv[1], "r");
    if (!file)
    {
        fprintf(stderr, "Could not open %s.\n", argv[1]);
        fclose(file);
        return 2;
    }
    char *filename = malloc(50 * sizeof(int));

    BYTE buffer[FAT];
    if (!filename)
    {
        return 3;
    }
    FILE *curImg;
    int count = 0;
    while (!feof(file))
    {
        fread(buffer, sizeof(buffer), 1, file);
        if (buffer[0] == 0xff && buffer[1] == 0xd8 && buffer[2] == 0xff && (buffer[3] & 0xf0) == 0xe0)
        {
            if (count > 0)
                fclose(curImg);

            sprintf(filename, "%03i.jpg", count);
            curImg = fopen(filename, "w");
            if (curImg)
                fwrite(buffer, sizeof(buffer), 1, curImg);
            count++;
        }
        else
        {
            if (curImg)
                fwrite(buffer, sizeof(buffer), 1, curImg);
        }
    }
    free(filename);
    fclose(file);
    return 0;
}

没看懂,有什么问题。谁能帮帮我?

发生在 'end of file indicator'。这意味着在写入文件后,有一段时间会在最后一个文件中写入一些东西。我只是在

中添加了一个条件
if (curImg)
     fwrite(buffer, sizeof(buffer), 1, curImg);

替换为

if (curImg && !feof(file))
     fwrite(buffer, sizeof(buffer), 1, curImg);