CS50恢复 - 恢复的图像不匹配

发布于 2025-01-27 01:58:42 字数 2202 浏览 2 评论 0原文

我在CS50课程中的第4周,恢复问题。我的代码恢复了50个看上去对我的JPG,我找不到我的错误,我不知道该怎么办。当我使用check50时,它返回:

:) recover.c exists.  
:) recover.c compiles.  
:) handles lack of forensic image  
:( recovers 000.jpg correctly  
   recovered image does not match  
:( recovers middle images correctly  
    recovered image does not match  
:( recovers 049.jpg correctly  
    recovered image does not match  

代码:

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

const int BLOCK_SIZE = 512;
typedef uint8_t  BYTE;

int main(int argc, char *argv[])
{

    if (argc != 2)
    {
        printf("Usage: ./recover IMAGE\n");
        return 1;
    }

    FILE *raw_file = fopen(argv[1],"r");

    if (raw_file == NULL)
    {
        printf("The image cannot be opened");
        return 1;
    }

    //buffer for storing 512 bytes of memory for cheking if there is jpg
    BYTE buffer[512];

    //variable that remember if i alredy created one file
    bool alredycreated = false;

    //variable of file
    int intfilename = 0;
    FILE* file = NULL;
    char filename[8];

    while (fread(buffer, 1, BLOCK_SIZE, raw_file) == BLOCK_SIZE)
    {
        if (buffer[0] == 0xff)
        {
            if (buffer[1] == 0xd8)
            {
                if (buffer[2] == 0xff)
                {
                    if (buffer[3] >= 0xe0 && buffer[3] <= 0xef)
                    {
                        if (alredycreated == true)
                        {
                            fclose(file);
                        }
                        sprintf(filename, "%03i.jpg",intfilename);
                        file = fopen(filename, "wb");
                        fwrite(&buffer, BLOCK_SIZE, 1, file);
                        alredycreated = true;
                        intfilename = intfilename + 1;
                    }
                }
            }
        }
        else if (alredycreated == true)
        {
            fwrite(&buffer, BLOCK_SIZE, 1,  file);
        }
    }
    fclose(raw_file);
}

Sorry for my english i am still learning english too.

I am in week 4 in CS50 course and I'm having problems with recover. My code recovers 50 jpgs that look right to me, and I can't find my mistake and I don't know what I'm supposed to do. When I use check50 it returns:

:) recover.c exists.  
:) recover.c compiles.  
:) handles lack of forensic image  
:( recovers 000.jpg correctly  
   recovered image does not match  
:( recovers middle images correctly  
    recovered image does not match  
:( recovers 049.jpg correctly  
    recovered image does not match  

Code:

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

const int BLOCK_SIZE = 512;
typedef uint8_t  BYTE;

int main(int argc, char *argv[])
{

    if (argc != 2)
    {
        printf("Usage: ./recover IMAGE\n");
        return 1;
    }

    FILE *raw_file = fopen(argv[1],"r");

    if (raw_file == NULL)
    {
        printf("The image cannot be opened");
        return 1;
    }

    //buffer for storing 512 bytes of memory for cheking if there is jpg
    BYTE buffer[512];

    //variable that remember if i alredy created one file
    bool alredycreated = false;

    //variable of file
    int intfilename = 0;
    FILE* file = NULL;
    char filename[8];

    while (fread(buffer, 1, BLOCK_SIZE, raw_file) == BLOCK_SIZE)
    {
        if (buffer[0] == 0xff)
        {
            if (buffer[1] == 0xd8)
            {
                if (buffer[2] == 0xff)
                {
                    if (buffer[3] >= 0xe0 && buffer[3] <= 0xef)
                    {
                        if (alredycreated == true)
                        {
                            fclose(file);
                        }
                        sprintf(filename, "%03i.jpg",intfilename);
                        file = fopen(filename, "wb");
                        fwrite(&buffer, BLOCK_SIZE, 1, file);
                        alredycreated = true;
                        intfilename = intfilename + 1;
                    }
                }
            }
        }
        else if (alredycreated == true)
        {
            fwrite(&buffer, BLOCK_SIZE, 1,  file);
        }
    }
    fclose(raw_file);
}

Sorry for my english i am still learning english too.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文