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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T16:35:12+00:00 2026-05-25T16:35:12+00:00

This is got me pretty stuck, how do I fix this? I know I

  • 0

This is got me pretty stuck, how do I fix this? I know I haven’t got error checking, but they aren’t required i’d guess since it’s restricted to my desktop. It obveously can’t be EOF. It’s for the infoheader struct, fileheader works fine. Do i need to take a new line or something?

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

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 */
}FILEHEADER; 

typedef struct                       
{ 
    unsigned int   biSize;            
    int            width;            /* Width of the image */ 
    int            height;           /* Height of the image */ 
    unsigned short planes;             
    unsigned short bitPix;             
    unsigned int   biCompression;      
    unsigned int   biSizeImage;        
    int            biXPelsPerMeter;    
    int            biYPelsPerMeter;    
    unsigned int   biClrUsed;          
    unsigned int   biClrImportant;     
}INFOHEADER; 

typedef struct                        
{ 
    unsigned char  b;         /* Blue value */ 
    unsigned char  g;         /* Green value */ 
    unsigned char  r;         /* Red value */ 
 }IMAGECOMPONENT; 

 int fileheadfunc(FILE *image);
 int infoheadfunc(FILE *image);

 int main( int argc, char *argv[] )
 {
    char *filename; /* *threshholdInput = argv[2]; */
    FILE *image;
    int filehead, infohead;
    filename = argv[1];
    /* int threshhold = atoi(threshholdInput); */

    if (argc != 2) 
    {
              printf(" Incorrect Number Of Command Line Arguments\n");
              return(0);
    }

    image = fopen( filename, "r");

        if (image == NULL)
    {
    fprintf(stderr, "Error, cannot find file %s\n", filename);
    exit(1);
    }

    filehead = fileheadfunc(image);
    infohead = infoheadfunc(image);
    fclose(image);

   return(0);             
}

int fileheadfunc(FILE *image)
{
    FILEHEADER *header;
    long pos;

    fseek (image , 0 , SEEK_SET);

    fread( (unsigned char*)header, sizeof(FILEHEADER), 1, image );


    if ( (*header).fileMarker1 != 'B'  || (*header).fileMarker2 != 'M' )
    {
    fprintf(stderr, "Incorrect file format");
    exit(1);
    }

    printf("This is a bitmap!\n");
    pos = ftell(image);
printf("%ld\n", pos);
printf("%zu\n", sizeof(FILEHEADER));

return(0);
}

int infoheadfunc(FILE *image)
{
    INFOHEADER *iheader;

    fseek (image, 0, SEEK_CUR ); 
    fread( (unsigned int*)iheader, sizeof(INFOHEADER), 1, image );

    printf("Width: %i\n", (*iheader).width);
    printf("Height: %i\n", (*iheader).height);

    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-05-25T16:35:13+00:00Added an answer on May 25, 2026 at 4:35 pm

    There are two problems with the code:

    Alignment

    For performance reasons the compiler will, unless instructed to do otherwise, arrange struct fields on its "natural boundaries", effectively leaving uninitialised gaps between byte-size fields. Add

    #pragma pack(1) 
    

    before the struct definitions and you should be fine. It’s also easy to test: just print out the struct size without and with pragma pack in place, and you’ll see the difference.

    Allocation

    As Paul R already said, you should allocate space for the headers, not just provide a pointer to the structures. The fact that fileheadfunc works is a coincidence, there just wasn’t anything in the way that got smashed when data got written outside of the allocated space.

    A last one, just for prevention sake: should you ever want to return the read structures to the calling program, do not just return a pointer to the structure allocated in the function as that will cause problems similat to the unallocated variables you have now. Allocate them in the calling function, and pass a pointer to that variable to the header read functions.

    EDIT clarification regarding the last point:

    DON’T

    FILEHEADER * fileheadfunc(FILE *image)
    {
        FILEHEADER header;
        ...
        return &header; // returns an address on the function stack that will 
                        // disappear once you return
    }
    

    DO

    int fileheadfunc(FILE *image, FILEHEADER *header)
    {
        ...
    }
    

    which will be called like this

    ...
    FILEHEADER header;
    returnvalue = fileheaderfunc(imagefile,&header);
    

    EDIT2: just noticed that the way you read the DIB header is not correct. There are several variations of that header, with different sizes. So after reading the file header you first need to read 4 bytes into an unsigned int and based on the value read select the correct DIB header structure to use (don’t forget you already read its first field!) or tell the user you encountered an unsupported file format.

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

Sidebar

Related Questions

I'm sure the answer is pretty simple, but I got stuck in this: Welcome
I'm stuck with this pretty silly thing; I got a textfile like this; Hello::140.0::Bye
this is probably pretty simple, but I've got this text file containing a bunch
I'm goofing around with the new youtube as3 API but got stuck. This is
I got stuck with a problem that looked pretty easy but i cant make
I've got the following situation: <h2>This text is <span>pretty awesome</span></h2> I'm trying to give
This has got to be something I just missed, but how do I add
I got this error today when trying to open a Visual Studio 2008 project
I'm pretty much stuck with a question I never got an answer for, a
This should be pretty easy but I'm having a heck of a time doing

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.