1

I need to read 16 bits from the binary file as std::string or char *. For example, a binary file contains 89 ab cd ef, and I want to be able to extract them as std::strings or char *. I have tried the following code:

ifstream *p = new ifstream();
char *buffer;  
p->seekg(address, ios::beg);  
buffer = new char[16];  
memset(buffer, 0, 16);  
p->read(buffer, 16);  

When I try to std::cout the buffer, nothing appeared. How can I read these characters in the binary file?

EDIT: I was looking for the buffer to be a int type such as "0x89abcdef". Is it possible to achieve?

7
  • What is the value of address when this code executes? Commented Jun 14, 2012 at 0:46
  • 3
    Did you open the file? Also, probably a better idea to declare *p on the stack: ifstream p("somefile.txt"); Commented Jun 14, 2012 at 0:47
  • What are you hoping to get in your buffer? Do you want it to contain four bytes with values 0x89, 0xAB, 0xCD, 0xEF respectively? Or are you trying to get some textual representation of those values (e.g., a string like "89 ab cd ef")? Commented Jun 14, 2012 at 0:48
  • 1
    If you're in Linux you can use strings bin_file_path to get a list of all the strings from a binary file Commented Jun 14, 2012 at 0:49
  • 1
    When you attempt to read data from a file, you should always check whether the attempt succeeded. For instance, when ifstream::read fails to read as many bytes as you asked for it sets some flags on the stream object which you can check with ifstream::fail and ifstream::eof. Commented Jun 14, 2012 at 0:52

3 Answers 3

4

Something like:

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

int main()
{
    if (ifstream input("filename"))
    {
        std::string s(2 /*bytes*/, '\0' /*initial content - irrelevant*/);
        if (input.read(&s[0], 2 /*bytes*/))
            std::cout << "SUCCESS: [0] " << std::hex << (int)s[0] << " [1] " << (int)s[1] << '\n';
        else
            std::cerr << "Couldn't read from file\n";
    }
    else
        std::cerr << "Couldn't open file\n";
}
Sign up to request clarification or add additional context in comments.

Comments

0

You can't read a binary stream as though it were text.

You can, of course, read as binary (by using "file.read()" and "file.write()" methods on your stream object). Just like what you're doing now :)

You can also convert binary to text: "convert to hex text string" and "uuencode base 64" are two common ways to do this.

Comments

0

You'll want to read the bytes as numbers (of type long long probably). Then you can print those using formatting specifiers like this:

#include <iostream>
#include <iomanip>

int main()
{
    using namespace std;

    int x =   2;
    int y = 255;

    cout << showbase // show the 0x prefix
         << internal // fill between the prefix and the number
         << setfill('0'); // fill with 0s

    cout << hex << setw(4) << x << dec << " = " << setw(3) << x << endl;
    cout << hex << setw(4) << y << dec << " = " << setw(3) << y << endl;

    return 0;
}

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.