CS50的PSET 3-Recover.c-恢复了我的JPEG文件,但它们都为空

问题描述 投票:0回答:1

所以我目前正在尝试从cs50 pset3中恢复起来,并且我已经恢复了所有49个jpeg文件。但是,所有这些jpeg文件都是空的(带有灰色和白色网格)。有人可以解释我的代码出了什么问题吗?我尝试用check50查看我的代码是否正确,但是它说恢复的图像不匹配。

我也在fopen函数中将“ w”也更改为“ wb”,但这似乎也不起作用。

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

int main(int argc, char *argv[])
{
//a data type that can store a byte
typedef uint8_t BYTE;

//Checking to see if there is only one command line argument
if (argc != 2)
{
    fprintf(stderr, "Usage: ./recover image\n" );
    return 1;
}

//Opening the file to see if its correct

char *infile = argv[1];
FILE *memory = fopen(infile, "r");
if (memory == NULL)
{
    fprintf(stderr, "Could not open %s.\n", infile);
    return 2;
}

//Creation of a buffer
BYTE buffer[512] = {0};
//Whether or not we have found a JPEG or not
bool jpegfound = false;
//the number of JPEG files found
int numJPEGfile = 0;
//declaring the new to be JPEG file so that it has a scope for the 
whole while loop
FILE *img = NULL;
//declaring the new JPEG filename
char filename[8];

//Repeating until the end of card
while(fread(buffer, 512, 1, memory) == 1)
{

    //Start of a new JPEG?
    if (buffer[0] == 0xff && buffer[1] == 0xd8 && buffer[2] == 0xff && (buffer[3] & 0xf0) == 0xe0)
    {
        jpegfound = true;
        sprintf(filename, "%03i.jpg", numJPEGfile);
        numJPEGfile += 1;
        img = fopen(filename, "wb");
        fwrite(buffer, 512, 1, img);

    }

    //Have we already found a JPEG?
    if(jpegfound)
    {
        jpegfound = false;
        fclose(img);

    }


}

//Close any remaining files
fclose(memory);
return 0;

}
jpeg cs50
1个回答
0
投票

如果在终端中运行以下命令,恢复的jpg文件有多大?我认为这将为您提供解决此pset的提示。

ls -l
© www.soinside.com 2019 - 2024. All rights reserved.