我有一个大文件,里面包含很多 jpeg。所有的jpeg都需要一张一张的提取出来。这是我解决这个问题的第一步:
1)定义“块”变量并为其分配512字节的存储空间
2)打开包含所有jpeg的文件并循环遍历它直到eof
3) 抓取第一个块(512 字节)并查看里面有什么
目前我的代码无法编译。这是我的错误:
recover.c:27:19: error: implicitly declaring C library function
'malloc' with type 'void *(unsigned int)' [-Werror]
char* block = malloc(BYTE * 512);
^ recover.c:27:19: note: please include the header <stdlib.h> or explicitly provide a declaration for 'malloc'
recover.c:27:26: error: unexpected type name 'BYTE': expected
expression
char* block = malloc(BYTE * 512);
^ recover.c:45:18: error: conversion specifies type 'int' but the argument has type 'char *'
[-Werror,-Wformat]
printf("%c", block);
~^ ~~~~~
%s
到目前为止,这是我的代码:
#include <stdio.h>
#include <stdint.h>
typedef uint8_t BYTE;
//SOI - 0xFF 0xD8
//EOI - 0xFF 0xD9
//APPn - 0xFF 0xEn
int main(void)
{
//FAT - 512 bytes per block
char* block = malloc(BYTE * 512);
//open file containing pictures
FILE* fh = fopen("card.raw", "rd");
//make sure the file opened without errors
if (fh == NULL)
{
printf("something went wrong and file could not be opened");
return 1;
}
while (!feof(fh))
{
setbuf(fh, block);
printf("%c", block);
}
fclose(fh);
return 0;
}
我究竟做错了什么?为什么不是 char* block = malloc(BYTE * 512); 分配 512 个字节,但会引发错误?另外,由于我什至还不能编译这一段,我是否正确读取了 512 个字节?如果没有,我该怎么做呢?
谢谢。