gpt4 book ai didi

CS50:recover.c ~ 什么数据类型可以保存 JPG 的字节(具体为 512 个字节)?

转载 作者:行者123 更新时间:2023-11-30 19:16:50 26 4
gpt4 key购买 nike

我正在尝试从损坏的存储卡中恢复文件(JPG),这是我在哈佛 CS50 中遇到的问题。我应该创建一个缓冲区(512 字节长),从存储卡读取到缓冲区,然后查看缓冲区是否以与 JPG 相同的内容开头。这是:

  /**
* recover.c
*
* Computer Science 50
* Problem Set 4
*
* Recovers JPEGs from a forensic image.
*/

//0xff 0xd8 0xff 0xe0
//0xff 0xd8 0xff 0xe1

#define BLOCK 512
#define START1END 0xe0
#define START2END 0xe1

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

//making variables
int found = 0;
char* title;
FILE* img;
int ifopen = 1;

FILE* buffer[512];

int main(int argc, char* argv[])
{
//opening file
FILE* inptr = fopen("card.raw", "r");
//checking if file opening failed
if (inptr == NULL)
{
return 2;
}
//sets the begins or jpgs
uint8_t checkjpg1[4] = {0xff, 0xd8, 0xff, 0xe0};
uint8_t checkjpg2[4] = {0xff, 0xd8, 0xff, 0xe1};

//making buffer
unsigned char buffer;

//going through the file
while(fread(&buffer,sizeof(char),BLOCK,inptr) == BLOCK)
{
//checking if begin == the possible begin of jpg
if ((buffer[0] == checkjpg1[0] && buffer[1] == checkjpg1[1] && buffer[2] == checkjpg1[2]) &&
(buffer[3] == checkjpg1[3] || buffer[3] == checkjpg2[3]))
{
//if a jpg is not open
if (ifopen == 1)
{
//make one
found+=1;
sprintf(title,"00%d",found);
img = fopen(title,"a");
}
else//else
{
//end the one and open new one
fclose(img);
sprintf(title,"00%d",found);
img = fopen(title,"a");
}
}

fwrite(img,sizeof(char),BLOCK,&buffer);
}

fclose(inptr);
}

我想我已经记下来了,还是我还差得远?它一直给出这个错误:

    recover.c:70:40: error: incompatible pointer types passing 'unsigned char *' to
parameter of type 'FILE *' (aka 'struct _IO_FILE *')
[-Werror,-Wincompatible-pointer-types]
fwrite(img,sizeof(char),BLOCK,&buffer);
^~~~~~~
/usr/include/stdio.h:716:38: note: passing argument to parameter '__s' here
size_t __n, FILE *__restrict __s);
^

但是当我将“unsigned char”更改为 FILE* 时,它会抛出相同的错误,但使用 FILE*。它在要求什么?预先感谢您,我仍在努力掌握 CS!

最佳答案

你会使用

unsigned char buffer[512];

保存您读取的数据的变量。

fwrite()的第四个参数是要写入的流,而不是要写入的缓冲区。所以你可能有

fwrite(buffer,sizeof(char),BLOCK,img);

而不是相反地使用缓冲区和 img。但是,您声明的缓冲区不合适。

有两个称为 buffer 的变量,一个是 512 个 FILE * 指针的数组,另一个是单个无符号字符。 main() 中的代码无法看到前一个变量,因为 main() 中声明的变量隐藏了它。但该变量的长度为 1 个字节,您不会在其中保存 512 个字节的任何内容。您需要一个包含 512 个无符号字符的数组。

关于CS50:recover.c ~ 什么数据类型可以保存 JPG 的字节(具体为 512 个字节)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29269150/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com