Originally Posted by
nonoob
You shouldn't use 'int' in the structs. The BMP file specification says dword which means 32 bit. It's better to use 'long' because 'int' may have different sizes depending on platform.
Also you must use #pragma pack(1) or whatever is appropriate for your compiler to ensure that there is no structure element padding.
After INFOHEADER there is a palette array followed by image data.
Thanks for your reply. Regarding using int in the structs - this is the format given in the assignment outline, so i have no choice
I attempted to use #pragma pack (just for the 1 structure) see code below; however, for some reason bfSize was getting the wrong value. Values were outputted as followed, B > M > 921656 > 0 > 0 > 54 (all right except bfsize, 921656). Any ideas why ?
Code:
#include <stdio.h>
#pragma pack(push, 1);
typedef struct
{
unsigned char fileMarker1; /* 'B' */
unsigned char fileMarker2; /* 'M' */
unsigned int bfSize;
unsigned short unused1;
unsigned short unused2;
unsigned int imageDataOffset; /* Offset to the start of image data */
}header;
#pragma pop;
int main ( int argc, char *argv[] )
{
char filename[256];
FILE *image;
header data;
printf("Enter File Name: \n"); scanf("%s", &filename);
image = fopen(filename, "rb" );
fread(&data, sizeof(unsigned char), sizeof(data), image);
if(image == NULL)
{
printf("The file cannot be opened.\n");
}
else
{
printf("fileMarker1 = %c\n", data.fileMarker1);
printf("fileMarker2 = %c\n", data.fileMarker2);
printf("bfSize = %d\n", data.bfSize);
printf("unused 1 = %d\n", data.unused1);
printf("unused 2 = %d\n", data.unused2);
printf("imagedataoffset = %d\n", data.imageDataOffset);
}
return 0;
think i may have worked something out