0

I have a binary file called "input.bin" where every character is of 4 bits. The file contains this kind of data:

0f00 0004 0018 0000 a040 420f 0016 030b
0000 8000 0000 0000 0000 0004 0018 0000

where 0f is the first byte.

I want to read this data and to do that, I am using the following code:

#include <string>
#include <iostream>
#include <fstream>

int main()
{
      char buffer[100];
      std::ifstream myFile ("input.bin", std::ios::in | std::ios::binary);
      myFile.read (buffer, 100);

      if (!myFile.read (buffer, 100)) {
        std::cout << "Could not open the required file\n";
      }
      else
      {
        for (int i = 0; i < 4; i++)
        {
          std::cout << "buffer[" << i << "] = " << static_cast<unsigned>(buffer[i]) << std::endl;
        }
        myFile.close();
      }
    return 0;
}

Currently I am printing just four bytes of data, and when I run it, I get this output:

buffer[0] = 0
buffer[1] = 24
buffer[2] = 0
buffer[3] = 0

Why is it not printing the value of 0f and just printing the value of 18 in index 1 whereas it is actually at index 6?

10
  • Write your output into a text file first, then open this file and check it with some smart text editor like NotePad ++, Sublime Text ect . Windows console is not the best thing to test characters. Commented Dec 1, 2021 at 13:58
  • You probably want to print in hex instead. 3 of the 4 characters you are printing are unprintable. Look at the low values in ASCII table: https://www.asciitable.com/ Commented Dec 1, 2021 at 13:58
  • cast to int before printing Commented Dec 1, 2021 at 13:59
  • << buffer[i] --> << static_cast<int>(buffer[i]) -- Then you actually see the decimal values, not box characters and blanks. Commented Dec 1, 2021 at 13:59
  • 3
    You're skipping over the first 100 bytes by reading twice. Commented Dec 1, 2021 at 14:24

2 Answers 2

2

The problem is here

myFile.read (buffer, 100);

if (!myFile.read (buffer, 100)) {

where you read twice, and thus ignore the first 100 bytes (if there are more than 100 of them).

Remove the first read, or change the condition to if (!myFile)

Sign up to request clarification or add additional context in comments.

Comments

1

You print the contents of the data as characters. And none of the first four bytes are really printable characters.

You need to print them as (unsigned) integers:

// Unsigned bytes to avoid possible sign extensions in conversions
unsigned char buffer[100];

...

// Convert the bytes to unsigned int for printing their numerical values
std::cout << "buffer[" << i << "] = " << static_cast<unsigned>(buffer[i]) << '\n';

3 Comments

Changing the type of buffer from char to unsigned char gives me this error: error: invalid conversion from ‘unsigned char*’ to ‘std::basic_istream<char>::char_type* {aka char*}’ [-fpermissive] myFile.read (buffer, 100);
Any help with the above problem?
@MobiZaman Cast in the read call: reinterpret_cast<char*>(buffer).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.