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?

return 0
  • 4,226
  • 6
  • 47
  • 72
  • What is the value of `address` when this code executes? – Gareth McCaughan Jun 14 '12 at 00:46
  • 3
    Did you open the file? Also, probably a better idea to declare `*p` on the stack: `ifstream p("somefile.txt");` – Anthony Jun 14 '12 at 00: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"`)? – Gareth McCaughan Jun 14 '12 at 00: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 – higuaro Jun 14 '12 at 00:49
  • (Note: although your question would be improved if it addressed the points in my comments above, it is extremely likely that anthony-arnold's first question points to the main cause of your troubles.) – Gareth McCaughan Jun 14 '12 at 00: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`. – Gareth McCaughan Jun 14 '12 at 00:52
  • @GarethMcCaughan is right. `p->good()` is probably returning false. – Anthony Jun 14 '12 at 00:55

3 Answers3

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";
}
Tony Delroy
  • 102,968
  • 15
  • 177
  • 252
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.

paulsm4
  • 114,292
  • 17
  • 138
  • 190
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;
}
GGibson
  • 354
  • 2
  • 9