I am trying to create a BITMAP image using C++.
Code:
#include <stdio.h>
#include <fstream>
using namespace std;
struct BMPHeader{
short int file_type = 0x4d42;
int file_size = 0;
};
int main(){
struct BMPHeader img;
printf("%ld",sizeof(img.file_type));
ofstream f1;
f1.open("output.bmp", ios::out);
f1.write(reinterpret_cast <char *> (&img), sizeof(img));
f1.close();
return 0;
}
Now if I use xxd
to view the output.bmp file I get
$ xxd output.bmp
00000000: 424d 0000 0000 0000 BM......
Here the variable "file_type" becomes 4 bytes when stored. However the output of sizeof()
is 2bytes.
Now, if the variable "file_size" is not declared, then it's stored as 2 bytes(which is what I want).
struct BMPHeader{
short int file_type = 0x4d42;
//int file_size = 0;
};
xxd output.bmp
00000000: 424d BM
What am I doing wrong here?