0

I have problem with reading my binary file. When I read binary file that contains strings it reads perfectly. But when I try read file that looks something like this:

1830 3030 3030 3131 3031 3130 3000 0000
0000 0000 0000 0000 1830 3030 3030 3131
3030 3030 3100 0000 0000 0000 0000 0000
1830 3030 3030 3131 3030 3131 3000 0000
0000 0000 0000 0000 1830 3030 3030 3131
3031 3030 3000 0000 0000 0000 0000 0000
1830 3030 3030 3131 3031 3131 3100 0000
0000 0000 0000 0000 1830 3030 3030 3131
3130 3130 3100 0000 0000 0000 0000 0000 ... and so on 

it reads just portion of it. This is my code for reading and converting the binary file to string.

string toString (const char *c, int size);

int main(int argc, char* argv[]) 
{
    streampos size;
    char * memblock;

    ifstream file (argv[1], ios::in|ios::binary|ios::ate);
    size = file.tellg();
    memblock = new char[size];
    file.seekg (0, ios::beg);
    file.read (memblock, size);
    file.close();

    string input = toString(memblock,size);
    cout << input << endl; //this prints just portion of it 000001101100
    return 0;
}

string toString (const char *c, int size)
{
    string s;
    if (c[size-1] == '\0')
    {
        s.append(c);
    }
    else 
    {
        for(int i = 0; i < size; i++)
        {
            s.append(1,c[i]);
        }

    }

    return s;
}

But when I try to read txt file of 0 and 1 it reads just fine. I am pretty new to c++, and I'm not quite sure why is that.

3
  • If you tell us how you'd like the output to be given this data, we can probably write some code for you to show you how. Commented Jun 12, 2016 at 10:31
  • It also appears that you are not freeing memory allocated for memblock variable. Commented Jun 12, 2016 at 10:40
  • hmm... I actually get this file from compressing another .bin file with LZW. When I compressed it to .bin i get 1830 3030 3030 3131... but when it's compressed to .txt i get 000001101100. So that portion of .bin is the same as in .txt. When I decompress .txt file I get original without any mistakes. But I cant decompress .bin because is there is just the portion of it. I thought there was something worng with reading, and that it reads just block of it. Commented Jun 12, 2016 at 10:59

1 Answer 1

1

Your problem is that you're using cout. This is designed to print human-readable strings, not binary. So the line that you flagged:

cout << input << endl; //this prints just portion of it 000001101100

would only print a portion of it.

The binary data you gave was:

1830 3030 3030 3131 3031 3130 3000 0000

Here is the ASCII for the first line of the data:

<CAN> "000001101100" <NUL> <NUL> <NUL>

The first <CAN> is 0x18 - the <NUL> has the value 0 - and that's where cout stops: it prints human-readable ASCII values until it encounters a 0 - your data is full of them.

You need to print the hex values of the characters - a much more involved process.

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

Comments

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.