struct overlay buffer

Hello,

I currently try to read the header of a simple bitmap file. For that reason I open the file and read it into a buffer and set the 'structure-pointer' at it's address.
Afterwards I try to 'cout' the header values. For the first two characters (BM) everything went fine. But when it comes to the filelength I do not achieve to get the propper output.
Does any of you have a hint for me what I am doing wrong?

Below you can find my code with a description of the header.
The data in file "redsquare.bmp" starts with hex: "42 4D 86 AC 00 00 00 00 00 00 7A 00"

If I have overseen to provide additional necessary information please let me know. This is my first post into a forum.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#include <iostream>
#include <fstream>

using namespace std;


int main()
{
    // see bmp file definition at:
    // https://en.wikipedia.org/wiki/BMP_file_format



    struct BMP_HEADER{
        /*
        Header is optional = NO
        +-------------+------------+----------+-------------------------------------+
        | Offset hex  | Offset dec | Size     |  Purpose                            |
        +-------------+------------+----------+-------------------------------------+
        | 00          | 0          | 2 bytes  | 0x42 0x4d -> BM                     |
        +-------------+------------+----------+-------------------------------------+
        | 02          | 2          | 4 bytes  | Size of the BMP file in bytes       |
        +-------------+------------+----------+-------------------------------------+
        | 06          | 6          | 2 bytes  | Reserved                            |
        +-------------+------------+----------+-------------------------------------+
        | 08          | 8          | 2 bytes  | Reserved                            |
        +-------------+------------+----------+-------------------------------------+
        | 0A          | 10         | 4 bytes  | Offset where bmp data starts        |
        +-------------+------------+----------+-------------------------------------+
        */

        char        ftype[2];
        uint32_t    filesize;
        char        reserved1[2];
        char        reserved2[2];
        uint32_t    bmp_start;

    } *bmp_header;

    /* open file for read */
    ifstream FileBin("redsquare.bmp", ios::in|ios::binary);
    if (FileBin.is_open()){

        // determine filesize
        FileBin.seekg(0, ios::end);
        int flength = FileBin.tellg();

        cout << "File length (dec) " << flength
             << " (hex) "            << hex << flength << endl;

        //Go back to beginning of file that read can be performed properly
        FileBin.seekg(0, ios::beg);

        // create buffer and read file
        char *pBuffer = new char[flength];
        FileBin.read(pBuffer, flength);
        bmp_header = (BMP_HEADER*) pBuffer;

        // cout values
        cout << "Type: "        << bmp_header->ftype[0]
                                << bmp_header->ftype[1]        << endl;
        cout << "Filesize: "    << bmp_header->filesize        << endl;
        cout << "pBuffer : "    << hex << pBuffer[2]           << endl;

    }
    else {
        cout << "File is not open" << endl;

    }

    return 0;
}
I learned that eg. filesize referes to wrong address since fields in structure are aligned to full 4 byte addresses. For example a char(1) field is filled up with 3 additional 'unused' bytes. The next field in struct starts afterwards.
An alignment on byte level can be achieved by the compiler statement #pragma pack(1) . But this correction can have impact on performance.
Topic archived. No new replies allowed.