Hello fellow stackoverflowers.
I'm doing a little hobby project in C code where I would like to do sone processing on .BMP files. So with me I use this wikipedia page Bmp file format
But very quickly I stumped upon a question. I'm trying to read the first 14 bytes of the file, the bitmap file header. But when when I print the bytes read they are only of 8 bytes length? Is this because the other bytes are zero or am I doing something wrong?
From GDB:
(gdb) p pImageHeader
$3 = 0x602250 "BMz\270\v"
(gdb) x pImageHeader
0x600x602250: 0xb87a4d42
Here is my code:
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char* argv[])
{
// If arguments are less than 2, just quit and tell user how do use the god damn program!
if (argc < 2)
{
printf("Usage : %s xxxx.bmp \n", argv[0]);
exit(1);
}
// Create file pointer and attach it to argument file location
FILE* pImageFile;
pImageFile = fopen(argv[1], "r");
// if file doesn't open, quit the program
if (pImageFile == NULL)
{
printf("Some error occured openening file : %s \n", argv[1]);
exit(1);
}
char* pImageHeader;
pImageHeader = malloc(sizeof(char) * 14);
if (pImageHeader == NULL)
{
printf("Error asking for RAM \n");
exit(1);
}
const int HEADER_SIZE = 14;
size_t bytes_read = fread(pImageHeader, 1, HEADER_SIZE, pImageFile);
if (bytes_read < HEADER_SIZE)
{
printf("Something went wrong reading file header! \n");
exit(1);
}
int i = 0;
for (i; i != 14; i++)
{
printf("%02X ", pImageHeader[i]);
}
printf(" \n");
return 0;
}
EDIT: Change the source code to check up the amount of bytes actually read. It passes so it reads 14 bytes.