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

  • Home
  • SEARCH
  • 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 1042221
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T15:28:27+00:00 2026-05-16T15:28:27+00:00

I recently did a programming test in which there was a ifstream section which

  • 0

I recently did a programming test in which there was a ifstream section which I could not solve. Since then I have been trying to solve the problem in my free time to no avail.

The problem is basically to read from a binary file and extract the information there.

Here is the file format:

------------------------------------------------------------------------------
| File Offset (in Bytes)| Value Type                 | Value Description     |
------------------------------------------------------------------------------
| 0                     | Unsigned Integer (32 bits) | number of entities    |
------------------------------------------------------------------------------
| 4                     | Entity information (see    | Entity 0              |
|                       | below)                     |                       |
------------------------------------------------------------------------------
| 4+32                  | Entity Information         | Entity 1              |
------------------------------------------------------------------------------
| ...                   | ...                        | ...                   |
------------------------------------------------------------------------------
| 4 + (32 * N)          | Entity Information         | Entity N              |
------------------------------------------------------------------------------

Entity Information:
------------------------------------------------------------------------------
| Offsett (in Bytes)| Value Type                 | Value Description         |
------------------------------------------------------------------------------
| 0                 | Unsigned short (16 bits)   | Unique ID                 |
------------------------------------------------------------------------------
| 2                 | Unsigned short (16 bits)   | Entity type ID            |
------------------------------------------------------------------------------
| 4                 | Single-precision float (32 | Position X coordinate     |
|                   | bits)                      |                           |
------------------------------------------------------------------------------
| 8                 | Single-precision float (32 | Position Y coordinate     |
|                   | bits)                      |                           |
------------------------------------------------------------------------------
| 12                | Single-precision float (32 | Forward Normal X          |
|                   | bits)                      | Component                 |
------------------------------------------------------------------------------
| 16                | Single-precision float (32 | Forward Normal Y          |
|                   | bits)                      | Component                 |
------------------------------------------------------------------------------

And here is my code:

void readFile() 
{
  ifstream ifs ( "binaryFile.bin" , ifstream::in );

  while (ifs.good())
  {
    int numEntities = 0;
    ifs.read( (char*)&(numEntities), sizeof(unsigned short));

    for(int i=0; i<numEntities; i++)
    {
      unsigned short uID;
      unsigned short eID;
      float x;
      float y;
      float forward_x;
      float forward_y;

      ifs.read( (char*)&(uID), sizeof(unsigned short) );
      ifs.read( (char*)&(eID), sizeof(unsigned short) );
      ifs.read( (char*)&(x), sizeof(float) );
      ifs.read( (char*)&(y), sizeof(float) );
      ifs.read( (char*)&(forward_x), sizeof(float) );
      ifs.read( (char*)&(forward_y), sizeof(float) );
    }
  }
  ifs.close();
}

Thanks.

  • 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-16T15:28:27+00:00Added an answer on May 16, 2026 at 3:28 pm

    When you say you are having problems, what output are you seeing, and how is it different from what you expect to see?

    Some general advice: For one thing, if you are going to be reading in binary data, try opening the stream in binary mode.

    Also, your first read operation stores data into an int, but the length read was unsigned short. Was this intentional?

    When you cast your pointer to char* for the first parameter to read, try using an explicit reinterpret_cast<char *> instead.

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

Sidebar

Related Questions

I've been interested in hardware programming recently, but I have not started yet. I
Recently I did a Java programming exercise successfully which was sent by a recruiting
I have recently been reading up on the D programming language, and am interested
I recently did a git stash , then did some work on the branch
So i've been playing around a both with box-shadows, i recently did something just
Did any one have luck recently register Visual studio 2008 express C# product? Look
Recently I was debugging a poof-all-customer-data-is-gone problem. It did not take much time to
I recently inherited an iPhone app. The original developer did not understand memory management
I recently started programming my first Cocoa app. I have ran into a problem
Initial Question: A group of us (electronic engineering students - UK) have recently been

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.