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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T03:39:56+00:00 2026-05-19T03:39:56+00:00

This is my problem. I am reading a file in binary mode, appending the

  • 0

This is my problem. I am reading a file in binary mode, appending the bytes to an int array, and printing the values after. My problem is, when I cout my results, random characters are being attached in the stream.

comp.txt:

this text is a testt1

main.cpp:

#include <iostream>
#include <fstream>
#include <time.h>


using namespace std;

void read(ifstream& stream, unsigned int buf[], int size)
{
    for(int i = 0; i < size; ++i)
    {
        unsigned char temp[4] = {'\0', '\0', '\0', '\0'};
        stream.read((char*)temp, 4);
        cout << "Temp: " << temp << '\n';
        buf[i] = *((int*)temp);     
        cout << "Read: " << buf[i] << endl;
        memset(temp, '\0', 4);
    }
}

int main()
{

    // open file
    ifstream f;
    f.open("comp.txt", ios::binary);
    cout << "File opened. " << endl;

    // get size
    f.seekg(0, ios::end);
    int l = f.tellg();
    int length = (l / 4) + 1;
    f.seekg(0, ios::beg);
    cout << "Size found: L: " << l << " Length: " << length << endl;

    // allocate byte arrays
    unsigned int* buf = new unsigned int[length];
    memset(buf, '\0', 4*length);
    // unsigned short* key = new unsigned short[length];
    cout << "Preparing to read..." << endl;

    // read byte into short
    cout << "Reading..." << endl;
    read(f, buf, length);
    f.close();
    delete[] buf;
    cin.ignore(1000, 10);
    return 0;
}

Output:

C:\Users\daminkz\Desktop>encrypt
File opened.
Size found: L: 21 Length: 6
Preparing to read...
Reading...
Temp: this
Read: 1936287860
Temp:  tex☺
Read: 2019914784
Temp: t is☻
Read: 1936269428
Temp:  a t♥
Read: 1948279072
Temp: estt♦
Read: 1953788773
Temp: 1
Read: 49

Things to note:

  • temp is only 4 bytes, but 5 bytes are printed
  • 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-19T03:39:56+00:00Added an answer on May 19, 2026 at 3:39 am

    When you read in temp, you overwrite all 4 characters with data, leaving you without a NULL terminator. cout.operator<<(char*) expects a null-terminated string, so it’s printing as many characters as it can until it reaches a null terminator. Making temp be 5 characters long, all ‘\0’, but keeping number of bytes read at 4 will alleviate the problem.

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

Sidebar

Related Questions

Update: After some more reading I see that this problem is totally general, you
I'm having problems reading this one JPEG file using ImageIO.read(File file) - it throws
its me again with this project. im having a problem reading the text on
I have to read a binary file in python. This is first written by
Possible Duplicate: reading integers from binary file in python I've read the solution to
I´m having a problem, in my server, after I send a file with X
My code for reading binary file is: dataFile.open(fileName.c_str()); ifstream binData(Trafficlog_Data.txt, ios::in | ios::binary); //binary
I'm reading contents from a binary file. If I read in data elements as
I'm trying to read from a binary file with BinaryReader . This is how
This problem is same as asked in here . Given a list of coins,

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.