微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

PSET4 RECOVERY 无法正确恢复图像

如何解决PSET4 RECOVERY 无法正确恢复图像

我已经开始研究 PSET 4 RECOVERY。我的程序编译成功,它通过前 3 次测试没问题,但对于我的爱我似乎找不到问题。 它只是没有正确恢复照片,我尝试将 ptr 更改为 char,反之亦然,但无济于事。

代码如下:

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

typedef uint8_t BYTE;
int main(int argc,char *argv[])
{
    //check for number of arguments
    if (argc != 2)
    {
        printf("Usage: ./recover filename\n");
        return 1;
    }
    //initialize files
    char *input_name = argv[1];
    FILE *input = fopen(input_name,"r");
    if (input == NULL)
    {
        //check if file is openable
        printf("File %s Could not be opened!",input_name);
        return 1;
    }
    //initialize files and pointers
    BYTE file[512];
    int counter = 0;
    FILE *img = NULL;
    char img_name[8];
    //loop as long as files returns bytes
    while(fread(&file,512,1,input) == 1)
    {
        //check if file is jpeg
        if (file[0] == 0xff && file[1] == 0xd8 && file[2] == 0xff && (file[3] & 0xf0) == 0xe0)
        {
            //if a prevIoUs file is open,close it
            if (counter != 0)
            fclose(img);
        }
        //initialize new file
        sprintf(img_name,"%03i.jpg",counter);
        img = fopen(img_name,"w");
        counter++;
        //if jpeg is found,write
        if (counter != 0)
        {
            fwrite(&file,img);
        }
    }
        fclose(input);
        fclose(img);
        return 0;

感谢任何帮助

解决方法

因此,您可以进行此检查以查看是否找到了新图像的开头。如果您发现新图像,您想打开它。但是看看那个分支结束的地方。您检查它是否是新图像的开始,然后检查是否需要关闭图像。然后你结束两个分支。每次循环运行时,您都会无条件地打开一个新图像。整个,也是这里唯一的错误,是花括号的位置。

//check if file is jpeg
if (file[0] == 0xff && file[1] == 0xd8 && file[2] == 0xff && (file[3] & 0xf0) == 0xe0)
{
    //if a previous file is open,close it
    if (counter != 0)
    fclose(img);
} // <- !! what !!
//initialize new file
sprintf(img_name,"%03i.jpg",counter);
img = fopen(img_name,"w");
counter++;
//if jpeg is found,write

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?