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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T22:57:12+00:00 2026-05-31T22:57:12+00:00

I’m trying to read data from a binary file and put it into a

  • 0

I’m trying to read data from a binary file and put it into a struct.
The first few bytes of data.bin are:

03 56 04 FF FF FF ...

And my implementation is:

#include <iostream>
#include <fstream>

int main()
{
    struct header {
        unsigned char type;
        unsigned short size;
    } fileHeader;

    std::ifstream file ("data.bin", std::ios::binary);
    file.read ((char*) &fileHeader, sizeof header);

    std::cout << "type: " << (int)fileHeader.type;
    std::cout << ", size: " << fileHeader.size << std::endl;

}

The output I was expecting is type: 3, size: 1110, but for some reason it’s type: 3, size: 65284, so basically the second byte in the file is skipped. What’s happening here?

  • 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-31T22:57:14+00:00Added an answer on May 31, 2026 at 10:57 pm

    Actually the behavior is implementation-defined. What actually happens in your case probably is, there is a padding of 1 byte, after type member of the struct, then after that follows the second member size. I based this argument after seeing the output.

    Here is your input bytes:

    03 56 04 FF FF FF
    

    the first byte 03 goes to the first byte of the struct, which is type, and you see this 3 as output. Then next byte 56 goes to the second byte which is the padding hence ignored, then the next two bytes 04 FF goes to the next two bytes of the struct which is size (which is of size 2 bytes). On little-endian machine, 04 FF is interpreted as 0xFF04 which is nothing but 66284 which you get as output.

    And you need basically a compact struct so as to squeeze the padding. Use #pragma pack. But such a struct would be slow compared to the normal struct. A better option is to fill the struct manually as:

    char bytes[3];
    std::ifstream file ("data.bin", std::ios::binary);
    file.read (bytes, sizeof bytes); //read first 3 bytes
    
    //then manually fill the header
    fileHeader.type = bytes[0];
    fileHeader.size = ((unsigned short) bytes[2] << 8) | bytes[1]; 
    

    Another way to write the last line is this:

    fileHeader.size = *reinterpret_cast<unsigned short*>(bytes+1); 
    

    But this is implementation-defined, as it depends on the endian-ness of the machine. On little-endian machine, it most likely would work.

    A friendly approach would be this (implementation-defined):

    std::ifstream file ("data.bin", std::ios::binary);
    file.read (&fileHeader.type, sizeof fileHeader.type);
    file.read (reinterpret_cast<char*>(&fileHeader.size), sizeof fileHeader.size);
    

    But again, the last line depends on the endian-ness of the machine.

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

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I am trying to render a haml file in a javascript response like so:
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I am currently running into a problem where an element is coming back from
I am trying to loop through a bunch of documents I have to put
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
I have just tried to save a simple *.rtf file with some websites and
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function

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.