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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T15:59:10+00:00 2026-06-08T15:59:10+00:00

#include <stdio.h> #include <stdint.h> #include <stdlib.h> int main() { FILE* bmp = NULL; uint32_t

  • 0
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>

int main()
{
    FILE* bmp = NULL;
    uint32_t offset;
    uint8_t* temp = NULL;
    size_t read;
    unsigned int x_dim = 600, y_dim = 388;

    bmp = fopen("test_colour.bmp", "r");

    if (!bmp)
        return -1;

    /* Get the image data offset */
    fseek(bmp, 10, SEEK_SET);
    fgets((char*)&offset, 4, bmp);

    printf("Offset = %u\n", offset);

    temp = malloc(3*x_dim*y_dim*sizeof(uint8_t));

    if (!temp)
        return -1;

    /* Go the the position where the image data is stored */
    fseek(bmp, offset, SEEK_SET);

    /* Copy image data to array */
    printf("%u bytes requested!\n", 3*x_dim*y_dim);
    read = fread((void*)temp, sizeof(uint8_t), 3*x_dim*y_dim, bmp);
    printf("%Iu bytes read!\n", read);

    fclose(bmp);
    free(temp);

    return 0;
}

I’m using the above code to read the RGB data of a 24-bit per pixel BMP image to an array. The offset from the start of file where the image data starts (after the BMP header) is given at offset 10 according to the BMP specification. I get the following output when executing the above code.

Offset = 54
698400 bytes requested!
33018 bytes read!

The offset output seems to be correct because the file size is 698454 bytes (=698400+54). However, the value returned by fread() seems to indicate that not the entire image data could be read. However, I’m subsequently using the data in the temp array to convert the RGB data to greyscale and writing this data to a BMP file again. Visually checking the output image does not indicate any errors, i.e. it seems as if I actually read the entire input image in the first place although fread() seems to indicate differently.

Can someone explain this behaviour?

  • 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-08T15:59:11+00:00Added an answer on June 8, 2026 at 3:59 pm

    (I’ll bet you’re on Windows)

    bmp = fopen("test_colour.bmp", "r");
    

    should be

    bmp = fopen("test_colour.bmp", "rb");
    

    If the file is opened in text mode on Windows, the runtime will stop reading when it happens to hit a 0x1a (Ctrl-Z) byte, which Windows considers an EOF marker for text files. Even if it doesn’t hit a Ctrl-Z, you’ll get corrupted data when Windows converts CR/LF sequences into a single LF character.

    However, I can’t explain why you’re able to get a good image from the partial file read (just lucky?).

    You are able to render an image from the buffer because the fread() implementation does read the number of bytes you requested (or nearly so – the number gets rounded down to a multiple of some block size) into the buffer, then it scans the buffer looking for CR/LF sequences to convert and Ctrl-Z EOF flags.

    So even though fread() returns 33018, the buffer has actually been nearly completely written with data from the file. The data isn’t 100% correct (for example, some CR characters were probably discarded) or complete, but in this case it’s close enough to render an image that looks like the one you expected.

    Of course, this is simply an observation of how this particular runtime behaves currently – it may not always behave that way in the future (or even on all systems today).

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

Sidebar

Related Questions

#include <stdint.h> #include <stdio.h> #include <stdlib.h> char* createMSG(uint8_t i,uint32_t port); int strlen(char* tmp); uint32_t
#include<stdio.h> #include<stdlib.h> char* re() { char *p = hello; return p; } int main()
#include <stdio.h> #include <stdlib.h> int main(void) { int x; int *in, *begin; in =
#include<stdio.h> #include<math.h> int main(int argc, char **argv){ // read in the command-line argument double
#include <stdio.h> #include <stdlib.h> int main(int argc, char **argv) { if(argc != 2) return
I am thinking of something like: #include <stdio.h> #include <conio.h> #include <stdlib.h> int main(void)
So basically: #include <stdio.h> #include <conio.h> #include <stdlib.h> #include <string.h> int main(void){ //test strrev
#include<stdio.h> #include<unistd.h> #include<stdlib.h> int main(int argc,char **argv) { int fd[2]; pid_t childpid; pipe(fd); childpid=fork();
#include <stdio.h> void wrapperPrint(char* s) { printf(s); return; } int main() { wrapperPrint(Hello world\n);
#include<stdio.h> #define N (sizeof(array) / sizeof(array[0])) main() { int array[5]={1,2,3,4,5}; int d; for(d=-1;d <=

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.