При компиляции использовать ключ –pack-struct
// Programming 1. Sample at 2017-05-04-14-20.53
#include <stdio.h>
union BMP_HEADER
{
char data[14];
struct
{
unsigned short signature;
unsigned int size;
unsigned int reserved12;
unsigned int offset;
} hdr;
};
class BmpImage
{
FILE *m_file;
char *m_data;
const char *m_name;
BMP_HEADER m_header;
public:
BmpImage()
{
m_file = NULL;
m_data = NULL;
m_name = NULL;
}
BmpImage(const char *name)
{
m_name = name;
m_file = NULL;
m_data = NULL;
printf("Hello, I'm created, my name is %s\n",m_name);
}
void print()
{
for(int i=0;i<sizeof(m_header);++i)
{
printf("%c(%x)",m_header.data[i],m_header.data[i]);
}
printf("[%u %u %u %u]\n",m_header.hdr.signature,
m_header.hdr.size,
m_header.hdr.reserved12,
m_header.hdr.offset);
}
bool open()
{
m_file = fopen(m_name, "rb");
if(NULL == m_file)
return false;
fread(&m_header.data,sizeof(char),sizeof(m_header.data),m_file);
// m_data = new char [???];
return true;
}
~BmpImage()
{
if(m_file)
fclose(m_file);
printf("bye I'm destroyed, my name was %s\n",m_name);
}
};
int main(/*int c, char **v */)
{
BmpImage b("image.bmp");
if(!b.open())
{
printf("Couldn't open image\n");
return -1;
}
printf("BMP_HEADER size = %ld\n",sizeof(BMP_HEADER));
b.print();
return 0;
}