sorry if formatting messy first time using stackoverflow. trying use fread
function read card.raw
file in blocks of 512 bytes , store first 4 bytes of block s1-s4
, other 508 bytes left on array of bytes. however, when try run segmentation fault. when tried debugging, after fread
function called, argv
, outptr
, inptr
, , buf
values become null
. why , doing wrong?
typedef uint8_t byte; //block of 512 bytes typedef struct { byte s1; byte s2; byte s3; byte s4; byte image[508]; } buffer; int main(int argc, char* argv[]) { //opens memory card file file* inptr = fopen("card.raw", "r"); int jpgcounter = 0; //creates jpg file char title[7]; sprintf(title, "%d.jpg", jpgcounter); file* outptr = fopen(title, "a"); //create buf (pointer) of type buffer buffer* buf; { //creates buffer structure storage of 512b blocks buf = malloc(sizeof(buffer)); //read 512 bytes @ time & stores every 512b block buffer struct buf fread(&buf, sizeof(buffer), 1, inptr); if(buf->s1 == 0xff && buf->s2 == 0xd8 && buf->s3 == 0xff) {
the if statement used check if first 3 elements of buf
contain following hexadecimal signatures. wanted use malloc
in loop in order read raw file in structs of 512 bytes, decide 512 bytes, free continuously.
use fread(buf, sizeof(buffer), 1, inptr);
instead of fread(&buf, sizeof(buffer), 1, inptr);
open file in binary mode read i.e. "rb"
instead of "r"