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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T22:46:05+00:00 2026-05-20T22:46:05+00:00

My code for reading binary file is: dataFile.open(fileName.c_str()); ifstream binData(Trafficlog_Data.txt, ios::in | ios::binary); //binary

  • 0

My code for reading binary file is:

dataFile.open(fileName.c_str());
ifstream binData("Trafficlog_Data.txt", ios::in | ios::binary);  //binary data file 
if(!binData) { 
  cout << "Cannot open file.\n"; 
  return -1; 
} 
char *memblock;int nBytes =12;
memblock = new char [nBytes+1];
binData.read(memblock,nBytes);
memblock[nBytes+1]='\0';
std::string message;message.assign(memblock,nBytes);
printf("%s\n",message.c_str());

Now i have given a file as input which contain binary and ascii data
RFB 003.003
RFB 003.003
–some binary data–

When I am reading first 12 bytes of file which is “RFB 003.003\n” but it prints “RFB 003.003=”. Can anyone tell where i m getting it wrong please.
promblem is not with ‘\0’. problem is it is not reading “RFB 003.003\n” . IS it because this file is mix of binary and ascii data

  • 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-20T22:46:06+00:00Added an answer on May 20, 2026 at 10:46 pm

    Change:

    memblock[nBytes+1]='\0';
    

    to:

    memblock[nBytes]='\0';
    

    Let’s say you read in six bytes to memblock, that goes into positions 0 through 5 inclusive:

      0   1   2   3   4   5    6    7
    +---+---+---+---+---+----+---+---+
    | H | E | L | L | O | \n | ? | ? |
    +---+---+---+---+---+----+---+---+
    

    (the ? areas still contain whatever rubbish was there before).

    You then need to put the null terminator at position 6 rather than position 7 as your code is doing.

    By placing the null terminator too far to the “right”, you’re including that first ? position, which could be holding anything.


    That’s what’s causing your specific problem. You also have an issue that you’re not allocating space to hold the data you’re reading in. You just have a char * but you’re not actually initialising it to point to usable memory. That’s almost certainly going to cause problems.

    Probably the simplest solution is to define it as:

    char memblock[nBytes+1];
    

    Although I see you’ve fixed that in your question now so it’s of little consequence. The actual problem (putting the null byte at the wrong location) is covered above.

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

Sidebar

Related Questions

I am wrapping up a class which reading a custom binary data file and
I am working with a binary file structure. The code example for reading the
I use this code fragment to read a binary file into a array fid=fopen('data.bin','rb')
I managed to parse a large binary file (~8Gb) by reading blocks of data
While reading a binary file using DJGPP on DOS this code hangs. This happens
In an Open Source program I wrote , I'm reading binary data (written by
I'm reading contents from a binary file. If I read in data elements as
The problem of reading (with c++ program) binary file generated by fortran code has
I have some code reading a text file and scanning for words between brackets:
I have the following code reading a file containing unicode text (Japanese). File f

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.