CS50:recover.c ~ 什么数据类型可以保存 JPG 的字节(具体为 512)?
CS50: recover.c ~ What data type would hold bytes (512 to be specific) of a JPG?
我正在尝试从损坏的存储卡中恢复文件 (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()的第4个参数是你要写入的流,而不是你要写入的缓冲区。所以你可能有
fwrite(buffer,sizeof(char),BLOCK,img);
而不是让缓冲区和 img 反过来。但是,您声明的缓冲区不合适。
您有两个名为缓冲区的变量,一个是包含 512 个 FILE * 指针的数组,另一个是单个 unsigned char。您在 main() 中的代码看不到前一个变量,因为在 main() 中声明的变量隐藏了它。但是那个变量是 1 字节长,你不会在其中保存 512 字节的任何东西。您需要一个包含 512 个无符号字符的数组。
我正在尝试从损坏的存储卡中恢复文件 (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()的第4个参数是你要写入的流,而不是你要写入的缓冲区。所以你可能有
fwrite(buffer,sizeof(char),BLOCK,img);
而不是让缓冲区和 img 反过来。但是,您声明的缓冲区不合适。
您有两个名为缓冲区的变量,一个是包含 512 个 FILE * 指针的数组,另一个是单个 unsigned char。您在 main() 中的代码看不到前一个变量,因为在 main() 中声明的变量隐藏了它。但是那个变量是 1 字节长,你不会在其中保存 512 字节的任何东西。您需要一个包含 512 个无符号字符的数组。