0

I have a binary file with the following data,

10110100
11111001
01110001

and I used this code to read it,

#include<bits/stdc++.h>
using namespace std;
int main()
{
    string in;
    cin>>in;
    ifstream input(in.c_str(),ios::in | ios::binary);
    if(!input.is_open())
    {
        cout<<"oops!!"<<endl;
        return 0;
    }
    cout<<"OK!!"<<endl;
    char a;
    while(input.get(a))
    {
        string s="";
        int b=a,c=0;
        while(b!=0)
        {
            c++;
            s+=(b%2?'1':'0');
            b/=2;
        }
        s+=string(8-c,'0');
        for(int i=s.length()-1;i>=0;i--)
            cout<<s[i];
        cout<<endl;
    }
    cout<<endl<<"DONE!!"<<endl;
}

Which basically reads a character, 1 byte, from the file using get() function and outputs it as a binary representation. I also tried read() but it didn't seem to work. I get the following output if it helps,

01001100
00000111
01110001

There is no other data in the file, I checked with a binary editor. What problem am I having?

genpfault
  • 51,148
  • 11
  • 85
  • 139
FieryRMS
  • 91
  • 3
  • 11
  • 1
    And when you used your debugger to run your program, what did you see? This is what a debugger is for. If you don't know how to use a debugger this is a good opportunity to learn how to use it to run your program one line at a time, monitor all variables and their values as they change, and analyse your program's logical execution flow. Knowing how to use a debugger is a required skill for every C++ developer, no exceptions. With your debugger's help you should be able to quickly find all bugs in this and all future programs you write, without having to ask anyone for help. – Sam Varshavchik Feb 11 '20 at 15:47
  • What do you expect the output to be? Should it be the same as the input? – Alan Birtles Feb 11 '20 at 15:54
  • 1
    See [Why should I not #include ?](https://stackoverflow.com/questions/31816095/why-should-i-not-include-bits-stdc-h) – Thomas Matthews Feb 11 '20 at 15:59
  • Sam Varshavchik, No I am pretty sure I don't have any logical errors, just problems with input from files, my program gives correct values when reading from files with ascii characters, but it can't read properly when the file has arbitrary binary values. – FieryRMS Feb 11 '20 at 17:11

1 Answers1

1

10110100 is 180.

On most platforms char is a signed 8-bit number with the range -128 to 127.

When you read in 10110100 a is set to -76.

76 is 01001100 in binary which matches your output.

Changing char to unsigned char should fix your problem:

unsigned char a;
while(input.read(reinterpret_cast<char*>(&a), 1))
Alan Birtles
  • 32,622
  • 4
  • 31
  • 60
  • Sorry, but it didn't work, I am receiving an error, error: invalid static_cast from type 'unsigned char*' to type 'char*' – FieryRMS Feb 11 '20 at 17:05
  • try `reinterpret_cast` – Alan Birtles Feb 11 '20 at 17:57
  • where? If you mean in `while()` then I did. I copy pasted your lines and replaced `char a; while(input.get(a))` – FieryRMS Feb 12 '20 at 11:31
  • No, 2 errors came up, `error: cannot bind rvalue '(std::basic_istream::char_type)((long long int)(& a))' to 'std::basic_istream::char_type& {aka char&}'` and `error: invalid conversion from 'char*' to 'std::basic_istream::char_type {aka char}' [-fpermissive] ` – FieryRMS Feb 12 '20 at 13:11
  • try the new version – Alan Birtles Feb 12 '20 at 15:44