【问题标题】:CS50: Recover, images say unsupported format when openingCS50:恢复,图像打开时显示不支持的格式
【发布时间】:2020-08-06 15:41:57
【问题描述】:

当我运行 check50 时,它说图像不匹配。我已经坚持了2个小时,现在我不知道该怎么办。映像恢复,但是当我尝试打开它时,它说不支持该文件类型。当我运行 check50 时,它说图像不匹配。我不知道该怎么办。我是编码新手,我还是个初学者,所以如果这是一个愚蠢的问题,我深表歉意。这是我的代码:

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

typedef uint8_t BYTE;

#define BLOCK_SIZE 512
#define FILE_NAME_SIZE 8
bool is_start_new_jpeg(BYTE buffer[]);
int main(int argc, char *argv[])
{
    if (argc != 2)
    {
    printf("Usage: ./recover image\n");
    return 1;
    }
FILE *infile = fopen(argv[1], "r");
if (infile == NULL)
{
    printf("File not found\n");
    return 1;
}

BYTE buffer[BLOCK_SIZE];
int file_index = 0;
bool have_found_first_jpg = false; 
FILE *outfile;
while (fread(buffer, BLOCK_SIZE, 1, infile))
{
    if (is_start_new_jpeg(buffer))
    {
        if (!have_found_first_jpg)
        {
            have_found_first_jpg = true;
        }    
        else 
        {
            fclose(outfile);
        }
        
        char filename[FILE_NAME_SIZE];
        sprintf(filename, "%03i.jpg", file_index++);
        outfile = fopen(filename, "w");
        if (outfile == NULL)
        {
            return 1;
        }    
        fwrite(buffer, BLOCK_SIZE, 1, outfile);     
               
    }
    else if (have_found_first_jpg)
    {
        //keep writing on the previous file
        fwrite(buffer, BLOCK_SIZE, 1, outfile);
    }
    
}
fclose(outfile);
fclose(infile);

}
bool is_start_new_jpeg(BYTE buffer[])
{
    return buffer[0] = 0xff && buffer[1] == 0xd8 && buffer[2] == 0xff && (buffer[3] & 0xf0) == 0xe0;
}

谁能告诉我哪里出错了。谢谢

【问题讨论】:

    标签: cs50 recover


    【解决方案1】:

    消息的出现是因为每个签名块的第一个字节已变为 0xff。 is_start_new_jpeg 函数中有(可能)错字。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-08-08
      • 2021-12-20
      相关资源
      最近更新 更多