Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8822427
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T06:06:19+00:00 2026-06-14T06:06:19+00:00

I find myself writing a simple program to extract data from a bmp file.

  • 0

I find myself writing a simple program to extract data from a bmp file. I just got started and I am at one of those WTF moments.

When I run the program and supply this image: http://www.hack4fun.org/h4f/sites/default/files/bindump/lena.bmp

I get the output:

type: 19778
size: 12
res1: 0
res2: 54
offset: 2621440

The actual image size is 786,486 bytes. Why is my code reporting 12 bytes?

The header format specified in,
http://en.wikipedia.org/wiki/BMP_file_format matches my BMP_FILE_HEADER structure. So why is it getting filled with wrong information?

The image file doesn’t appear to be corrupt and other images are giving equally wrong outputs. What am I missing?

#include <stdio.h>
#include <stdlib.h>

typedef struct {
    unsigned short type;
    unsigned int size;
    unsigned short res1;
    unsigned short res2;
    unsigned int offset;
} BMP_FILE_HEADER;

int main (int args, char ** argv) {
    char *file_name = argv[1];

    FILE *fp = fopen(file_name, "rb");

    BMP_FILE_HEADER file_header;

    fread(&file_header, sizeof(BMP_FILE_HEADER), 1, fp);

    if (file_header.type != 'MB') {
        printf("ERROR: not a .bmp");
        return 1;
    }

    printf("type: %i\nsize: %i\nres1: %i\nres2: %i\noffset: %i\n", file_header.type, file_header.size, file_header.res1, file_header.res2, file_header.offset);
    fclose(fp);

    return 0;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T06:06:20+00:00Added an answer on June 14, 2026 at 6:06 am

    There are two mistakes I could find in your code.

    First mistake: You have to pack the structure to 1, so every type size is exactly the size its meant to be, so the compiler doesn’t align it for example in 4 bytes alignment. So in your code, short, instead of being 2 bytes, it was 4 bytes. The trick for this, is using a compiler directive for packing the nearest struct:

    #pragma pack(1)
    
    typedef struct {
        unsigned short type;
        unsigned int size;
        unsigned short res1;
        unsigned short res2;
        unsigned int offset;
    } BMP_FILE_HEADER;
    

    Now it should be aligned properly.

    The other mistake is in here:

    if (file_header.type != 'MB')
    

    You are trying to check a short type, which is 2 bytes, with a char type (using ''), which is 1 byte. Probably the compiler is giving you a warning about that, it’s canonical that single quotes contain just 1 character with 1-byte size.

    To get this around, you can divide this 2 bytes into 2 1-byte characters, which are known (M and B), and put them together into a word. For example:

    if (file_header.type != (('M' << 8) | 'B'))
    

    If you see this expression, this will happen:

    'M' (which is 0x4D in ASCII) shifted 8 bits to the left, will result in 0x4D00, now you can just add or or the next character to the right zeroes: 0x4D00 | 0x42 = 0x4D42 (where 0x42 is 'B' in ASCII). Thinking like this, you could just write:

    if (file_header.type != 0x4D42)
    

    Then your code should work.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I find myself writing file and directory utility functions all the time, and I
I often find myself writing one off queries to either answer someone's question or
I often find myself writing very simple classes instead of C-style structs. They typically
Sometimes I find myself writing really simple wrappers, where a wrappers method directly corresponds
I find myself writing delegates occasionally for really simple functions (take no arguments and
For my parents I am writing a simple program to copy files from their
I am writing a simple vga controller and I find myself repeating the same
I find myself writing code such as: foreach($array as $key => $value) { switch($key)
I constantly find myself writing similar code like the example below: if (object[Object Name]
This morning, I find myself writing something like: if (a == b == c):

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.