我正在尝试从损坏的存储卡恢复文件(JPG' s)我在哈佛大学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);
^
但是当我改变&#34; unsigned char&#34;到FILE *,它会抛出相同的错误但是使用FILE *。它要求什么?提前谢谢你,我仍然在努力控制CS!
答案 0 :(得分:1)
你会用
unsigned char buffer[512];
表示保存您读取的数据的变量。
fwrite()的第4个参数是要写入的流,而不是要写入的缓冲区。所以你可能有
fwrite(buffer,sizeof(char),BLOCK,img);
而不是使用缓冲区和img反过来。但是,您声明的缓冲区不合适。
你有两个叫做buffer的变量,一个是512个FILE *指针的数组,另一个是一个unsigned char。 main()中的代码无法看到前一个变量,因为main()中声明的变量隐藏了它。但是该变量长度为1个字节,您不会在其中保存512个字节的任何内容。你想要一个512个无符号字符的数组。