#include
Struct bitmapfileHeader_ {
Short Type; Int bfsize; short re1, re2; int off.
Struct BitmapInfo_ {Long Size; Long Width, Height; Short Planes, Bitcount; Long Comp, SizeImg; Long Xpels, Ypels; Long Used, Important;
Struct ColORTABLE_ {Char Blue, Green, RED, RES;
Void xxx () {file * f = fopen ("f: //projects//bmp//1.bmp", "rb"); if (f == null) / * Judgment file is open success * / {ShowMessage ("File Open Error"); Return;} fseek (f, 0, 0); BitmapFileHeader_ * bmph = new bitmapfileHeader _ (); if (Fgets ((char *) BMPH, SIZEOF (BitmapFileHeader_), f) == null) {ShowMessage ("File Read Error"; Return;}
Bitmapinfo_ * bmpi = new bitmapinfo _ (); if (fgets ((char *) BMPI, SIZEOF (BitmapInfo_), F) == NULL) {ShowMessage ("File Read Error2"); Return;}
Form1-> edit1-> text = inttostr (bmph-> bfsize); form1-> edit2-> text = inttostr (bmpi-> width); form1-> edit3-> text = INTTOSTR (BMPI-> height);
Fclose (f);
This is a simple program that reads BMP file header information written by BCB, but work is not working.
Show results
Edit1: 0
Edit2: 1073741824
Edit3: 16777216
Hard thinking can't explain, open the BMP file with Ultra Edit to find the beginning of the file is
42 4D 38 14 00 00 00 00 00 36 04 00 00 28 00
The file is right, 42 4d is the BMP file flag "BM", the next 38 14 00 00 is the file size, the int value is 0x1438 = 5176 bytes, the same as Windows, is right.
Later, I wrote a program to read the first my own 'b' and the second byte 'm', when I was correct, I became zero when I read SIZE, I only doubt that BitmapFileHeader_ structures match data The problem, in order to further verify, write another program to fill the BMP file header, write the disk, and then view Ultra Edit, the program code is like this.
#pragma argsused
#include
#include
Struct bitmapfileHeader_ {
Short Type; Int bfsize; short re1, re2; int off.
INT Main (int Argc, char * argv []) {file * f = fopen ("f: //abc.txt", "wb"); BitmapFileHeader_ * b = new bitmapfileHeader _ (); b-> type = 1; B-> bfsize = 1; b-> re1 = b-> RE2 = 1; b-> offbits = 1; if (f == null) {Printf ("xxx"); system ("pause); return 0 }
FWRITE (B, Sizeof (BitmapfileHeader_), 1, F); System ("Pause"); Return 0;}
Use Ultra Edit to view the discovery actually wrote 16 bytes, as follows
01 00 67 32 01 00 00 01 00 01 00 01 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
After writing Type, there is no reason. There is no reason, and there is no problem later. Suddenly remember that the forum has discussed the word alignment problem in the C language, it is suspected to be a problem with the byte, and to find BCB bytes online. Instruction, the original BCB default is 4 bytes aligned, and the byte is aligned to 1 byte with #pragma pack.
Plus #pragma Pack (1), the data read is correct.
The Pentium machine is a 32-bit system, the data bus width is 32 bits, and the CPU transmits 4 bytes from memory. In order to take a data, the access memory can be minimized, the compiler default structure type data is aligned in 4 bytes. This way to read the BitMapFileHeader_ structure requires 4 access memory (16 bytes read 4 bytes each time), followed by the data behind this structure, and it is aligned in memory. If there is followed up with a BitmapFileHeader_ structure, then After reading the BitmapFileHeader_, only 4 times access memory, if you use #pragma pack (1) to force 1 byte alignment, visit the first BitmapFileHeader_, it is still 4 times to access memory, but the next time the visit is followed. When BitmapFileHeader_ requires 5 access memory, and then the following data will be aligned.
Although the compiler is aligned with the compiler defaults, sometimes the user has its own needs, just like the situation above, then use the IDE command #pragma pack (1) forced not aligned. .