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 1056865
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T17:46:43+00:00 2026-05-16T17:46:43+00:00

I am trying to create a new bitmap file using C. This is a

  • 0

I am trying to create a new bitmap file using C. This is a struct for the .bmp file header.

#define uint16 unsigned short
#define uint32 unsigned long
#define uint8  unsigned char
typedef struct 
{
 uint16 magic;  //specifies the file type "BM" 0x424d
 uint32 bfSize;  //specifies the size in bytes of the bitmap file
 uint16 bfReserved1;  //reserved; must be 0
 uint16 bfReserved2;  //reserved; must be 0
 uint32 bOffBits;  
} BITMAPFILEHEADER;

In my program I am doing this.

main() {
FILE* fp;
fp = fopen("test.bmp", "wb");
 BITMAPFILEHEADER bmfh;
 BITMAPINFOHEADER bmih;

 bmfh.magic = 0x4d42; // "BM" magic word
 bmfh.bfSize = 70; 
 bmfh.bfReserved1 = 0;  
 bmfh.bfReserved2 = 0; 
 bmfh.bOffBits = 54; 
fwrite(&bmfh, sizeof(BITMAPFILEHEADER), 1, fp);
fclose(fp);
}

So, when I read my test.bmp file it should contain 14 bytes (size of the stuct) and the values should be

42 4d 46 00 00 00 00 00 00 00 36 00 00 00

But if I read the file it shows me 16 bytes:

42 4d 04 08 46 00 00 00 00 00 00 00 36 00 00 00

From where does this “04 08” come.?
My bmp file becomes corrupted.

My question is that in binary file I/O if I write a structure to a file and its size is not multiple of 4Bytes (32 bit), does it automatically change the structure?

Any idea how to get around with this?

  • 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-05-16T17:46:44+00:00Added an answer on May 16, 2026 at 5:46 pm

    Your structure is being padded. The 04 08 is a garbage value from your stack. You need to use whatever feature your compiler provides to pack the structure. In most cases you should be able to use #pragma pack(1):

    #pragma pack(1)  // ensure structure is packed
    typedef struct 
    {
       .
       .
       .
    } BITMAPFILEHEADER;
    #pragma pack(0)  // restore normal structure packing rules
    

    You can read about data structure padding on wikipedia.

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

Sidebar

Related Questions

Im trying to create new file on D: drive with c/c++ I found this
I'm trying to create a new project using tomcat 7.0.27 + JSF 2.1.7 +
I am trying to create a new web application using PowerShell. I keep getting
when I create a bitmap like this: var testImage = new Bitmap(320, 240); var
I'm trying to save a bitmap to file and a specific directly using a
Im trying to create a Bitmap from a Png file stored on the SD
i'm trying to create a Bitmap BitmapDrawable img = new BitmapDrawable(getResources(), res/drawable/wrench.png); Bitmap wrench
I am now trying to create new database manager under Fragment class. But unfortunately,
I am trying to create new Event objects to be persisted in the database
I am trying to create new rules profile in Sonar 2.9 with my checkstyle

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.