3

I have got a file that contains data for multiple jpegs (along with some garbage), i need to extract binary from this file, filter out the garbage and create jpegs. I know the starting binary sequence of the jpegs.

char buffer[30];
ifstream fin;
fin.open ("FILENAME.raw", ios::in | ios::binary);
while (!fin.eof())
{
    fin.read(buffer,30);
    cout<<buffer[2]<<endl;
}
fin.close();

Here i am trying to print the file in binary but, when i run this code, alien characters are printed on the screen.

3
  • 1
    Probably you have alien characters in your file. That's usually the case with binary data. Commented Oct 23, 2012 at 6:38
  • What's the contents of the file itself as well? Commented Oct 23, 2012 at 6:39
  • 6
    Seems everything's fine and you have no problem. And in fact you didn't ask a question. Commented Oct 23, 2012 at 6:42

2 Answers 2

2

I think problem here was "cout << buffer[2]" which was converting your binary information to charecters. Try int cast before , also you should use a static "unsigned char array" because binary data can be unsigned .That will work :

unsigned char buffer[ 30 ];
ifstream fin;
fin.open ("FILENAME.raw", ios::in | ios::binary);
while (!fin.eof())
{

    fin.read( (char*)( &buffer[0] ), 30 ) ;
    cout << (int)buffer[2] << " ";
}
fin.close();
return 0;

Also if you want to traverse the binary why you are just printing buffer[2].

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

Comments

1

You should double check the binary contents of the file, as your code seems perfectly fine.

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.