我有一個大文件,其中包含大量的jpegs。所有的jpegs都需要逐一提取。這裏是我在處理這個問題的第一個步驟:從文件一次讀取512個字節並檢查裏面的內容?
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字節嗎?如果不是,我該怎麼去做到這一點?
謝謝。
您所做的第一件事是包含stdlib.h,因此在作用域中有一個適當的malloc()原型。然後,檢查malloc()的返回值以確保成功。這應該讓你至少在正確的道路上開始。 – 2013-03-18 22:54:35
錯誤消息告訴你確切的問題;你需要'#include'。 –
2013-03-18 22:55:22
非常感謝:) – haosmark 2013-03-18 23:03:32