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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T12:02:47+00:00 2026-05-21T12:02:47+00:00

I am honestly really confused on reading binary files in C. My data is

  • 0

I am honestly really confused on reading binary files in C.

My data is in a format like:

  • int header
  • Cell[][] cells, 8×8 matrix
  • Each cell is just a short id and a sbyte z.

However, something is clearly messing up and none of the values are accurate.

  int i, j;
  Block block;

  // read 32 bits
  fread( &(block.header), sizeof( int ), 1, mapFile );

  // loop through to fill the 8x8 matrix
  for( i = 0; i < 8; i++ )
  {
    for( j = 0; j < 8; j++ )
    {
      // read 16 bits
      fread( &(block.cells[i][j].tileId), sizeof( short ), 1, mapFile );
      // read 8 bits
      fread( &(block.cells[i][j].z), sizeof( char ), 1, mapFile );

      printf( "[%i][%i]: %x %i", i, j, block.cells[i][j].tileId, block.cells[i][j].z );
    }
  }
  printf( "header: %i", block.header );

Output of above shows a bunch of lines with [n][n]: ffffa800 251.

My C# version works fine though:

            Block block;
            block.header = reader.ReadInt32();
            block.cells = new Cell[8,8];
            for( int i = 0; i < 8; i++ )
            {
                for ( int j = 0; j < 8; j++ )
                {
                    block.cells[i, j].tileId = reader.ReadInt16();
                    block.cells[i, j].z = reader.ReadSByte();
                }
            }

            reader.Close();

Output from that (correctly) shows [n][n]: a8 -5.

  • 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-21T12:02:48+00:00Added an answer on May 21, 2026 at 12:02 pm

    You should use more strictly defined types in C. For example, int32_t (stdint.h). It’s possible that your short is 32-bit (or even your int is 16-bit), albeit unlikely.

    It’s probably also worthwhile to show us how the Block struct is defined.

    For reference:

    SByte = int8_t

    Int16 = int16_t

    Int32 = int32_t

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

Sidebar

Related Questions

I must honestly say that I do not really understand much about casting, but
Honestly, now when we have so many javascript features on the frontend I really
This really is a noobish question, but I honestly don't know how to solve
I'm learning about HTML5, and honestly I can't say I'm really impressed. Semantics are
Honestly I'm very bad at CSS and not good at dealing with color /
I honestly have only started recently researching this so my knowledge is limited. I
I've honestly tried this left and right and still find that my mirror server,
What things have to be done before I can honestly tell myself my web
There are a number of email regexp questions popping up here, and I'm honestly
I honestly do not know what they are called. I have been unable to

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.