c - Why does fread not read into uint16_t value properly? -
when trying read in signature bmp file, , after calling fread, sigcontains 52428note 0xcccc instead of 19778, proper decimal value 0x4d42 or 'bm' proper header signature bmp file.
uint16_t sig; // 0. if of operations fail, free memory , return 0 // 1. open file file *f = fopen(file, "r"); if (f == 0) { printf("cannot open file\n"); } // 2a. read initial fields of header; verify file type (bm) correct. fseek(f, 0, seek_set); fread(&sig, sizeof(uint16_t), 1, f); // signature printf("%d\n", sig); if (sig != 0x4d42) { printf("not bmp file!\n"); return 0; } i tried reading in 1 byte @ time using uint8_t, many other solutions fixing problem.
it seems fread not writing sig because cc default format uninitialized memory using compiler using. appreciated.
here bmp file i'm trying read in: image
your code totally should work. you're allocating 2 bytes sig , reading 2 bytes beginning of file correctly. long these first 2 bytes bmor '0x4d42' should good.
my guess issue lies directory/file paths.
check make sure file passing in has correct filepath. note pass relative filepath need make filepath relative project's root directory.
hope helps!
Comments
Post a Comment