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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T16:57:33+00:00 2026-06-10T16:57:33+00:00

I am trying to write a program that reads in binary files using C++.

  • 0

I am trying to write a program that reads in binary files using C++. I am running into some unusual output that I was hoping for some help with.

I have a binary file that starts with these 4 bytes:

A1 B2 C3 D4 (verified using hexdump -C)

And here is the code I am using to read these 4 bytes in:

#include <iostream> // for reading and writing files
#include <fstream>

using namespace std;

char * buffer;
unsigned int LENGTH = 4;

int main(int argc, char ** argv)
{
    // open the binary file
    ifstream infile ("dump", ios::binary | ios::in);

    // create a buffer
    buffer = new char[LENGTH];

    // read LENGTH bytes from the file
    infile.read(buffer, LENGTH);

    // append the null byte to terminate the string
    buffer[LENGTH] = '\0';

    // loop over the 4 bytes read, and print
    for(int i = 0; i < LENGTH; i++)
    {
       printf("Buffer[%d] is %X\n", i, buffer[i]);
    }

    delete[] buffer;
    infile.close();


    return 0;
}

This program gives me these actual results:

Buffer[0] is FFFFFFA1
Buffer[1] is FFFFFFB2
Buffer[2] is FFFFFFC3
Buffer[3] is FFFFFFD4

BUT, I would expect these results:

Buffer[0] is A1
Buffer[1] is B2
Buffer[2] is C3
Buffer[3] is D4

Can anyone explain to me where the 3 0xFF bytes are coming from ? It only seems to be affecting the first 4 bytes of the file, the next 4 bytes print out as expected without any 0xFF bytes prepended.

  • 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-10T16:57:35+00:00Added an answer on June 10, 2026 at 4:57 pm

    The char buffer[i] is default promoted when passed as a variable argument. To get the right value, say static_cast<unsigned char>(buffer[i]).

    Moreover, buffer[LENGTH] is out of bounds and thus undefined behaviour.

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

Sidebar

Related Questions

So, I'm trying to write a c program that reads input piped into the
I'm trying to write a program that reads in entries from a file into
I'm trying to write a program that reads broadcasted UDP datagrams in linux.I'm a
I am trying to write a Java program that reads an input file consisting
I am trying to write a program that sends basic text files from one
I am trying to write a program that allows a binary to be run,
I'm trying to write a basic Erlang program that reads in a file in
I'm trying to write a program that reads 2 numbers from the user and
I'm trying to write a java program that reads data from an image and
I'm trying to write a program that reads 2 numbers from the user and

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.