I'm trying to read the header of a BMP file and then display it's contents.
struct BMP *bmp;
bmp = (struct BMP*)malloc(sizeof(struct BMP));
if(bmp)
{
fread(bmp,sizeof(struct BMP),1,bmpFile); //This does not work for me
//Then we display the contents
printf("#######CABECALHO DE UM ARQUIVO .BMP (BITMAP)#########\n");
printf("\n");
printf("Tipo de Arquivo (2 bytes): %c%c\n", bmp->id[0],bmp->id[1]);
printf("Tamanho do arquivo (4 bytes): %d Kb\n",bmp->filesize/1024);
printf("Reservado1 (2 bytes): %x\n",bmp->reserved[0]);
printf("Reservado2 (2 bytes): %x\n",bmp->reserved[1]);
printf("Tamanho do Cabecalho BMP: %d\n",bmp->headersize);
printf("Tamanho do Info Header (4 bytes): %d\n", bmp->infosize);
printf("Largura: (4 bytes): %d\n", bmp->width);
printf("Altura: (4 bytes): %d\n", bmp->height);
printf("Plane: (2 bytes): %x\n", bmp->plane);
printf("Bits por Pixel: (2 bytes): %x\n", bmp->bits);
printf("Compressao: (4 bytes): %d\n", bmp->compression);
printf("Tamanho da Imagem: (4 bytes): %d\n", bmp->imagesize/1024);
printf("X: (4 bytes): %d\n", bmp->x);
printf("Y: (4 bytes): %d\n", bmp->y);
printf("Nro de Cores : (4 bytes): %d\n", bmp->clrUsed);
printf("Nro de Cores Importantes : (4 bytes): %d\n", bmp->clrImportant);
printf("\n");
printf("#######FIM DO CABECALHO TOTAL DE 50 BYTES#########");
}
However the only accurate information it shows me is the first 2 bytes all the other values are incorrect.
If instead of reading the whole struct
struct fread(bmp,sizeof(struct BMP),1,bmpFile);
I read each element at a time
fread(&bmp->id[0],sizeof(char),1,bmpFile);
fread(&bmp->id[1],sizeof(char),1,bmpFile);
fread(&bmp->filesize,sizeof(int),1,bmpFile);
fread(&bmp->reserved[0],sizeof(short),1,bmpFile);
fread(&bmp->reserved[1],sizeof(short),1,bmpFile);
fread(&bmp->headersize,sizeof(int),1,bmpFile);
fread(&bmp->infosize,sizeof(int),1,bmpFile);
fread(&bmp->width,sizeof(int),1,bmpFile);
fread(&bmp->height,sizeof(int),1,bmpFile);
fread(&bmp->plane,sizeof(short),1,bmpFile);
fread(&bmp->bits,sizeof(short),1,bmpFile);
fread(&bmp->compression,sizeof(int),1,bmpFile);
fread(&bmp->imagesize,sizeof(int),1,bmpFile);
fread(&bmp->x,sizeof(int),1,bmpFile);
fread(&bmp->y,sizeof(int),1,bmpFile);
fread(&bmp->clrUsed,sizeof(int),1,bmpFile);
fread(&bmp->clrImportant,sizeof(int),1,bmpFile);
then all values are displayed correctly... So my question is why is that happening what is different when I read the whole struct at once.