To read some data (namely integers, floats and strings) from an ordinary binary file on your filesystem, which was also created on your architecture:
#include <binfile.h>
binifstream file("test.dat");
int i;
float f;
char string[256];
// Read a 32-bit integer
i = file.readInt(4);
// Read an IEEE-754 single (32 bits)
f = file.readFloat(binio::Single);
// Read a string until newline or max. number of chars exceeded
file.readString(string, 256, '\n');
To do I/O on a file that was created on an x86 machine (i.e. Little Endian, IEEE-754 floats), while your own architecture is something different:
#include <binfile.h>
binfstream file;
int i = 1234567;
long pos;
// open the file "x86.dat" for reading and writing, but do not create
// it if it's not there.
file.open("x86.dat", binfbase::NoCreate);
// Check if we could cleanly open the file and bail out, if we
// couldn't.
if(file.error())
return ERROR;
// Set Little Endian mode, with IEEE-754 floats.
file.setFlag(binio::BigEndian, false); // remove flag
file.setFlag(binio::FloatIEEE); // set flag
// as we just want a demonstration, we discard all read data right away.
// Read a 16-bit integer
file.readInt(2);
// Read an IEEE-754 double (64 bits)
file.readFloat(binio::Double);
// Remember our current position in the stream
pos = file.pos();
// Seek to the end of the file
file.seek(0, binio::End);
// Write our variable i, determining its size using sizeof()
file.writeInt(i, sizeof(i));
// Seek back to our former position
file.seek(pos);
// Read a byte from here
file.readInt(1);
// close the file again
file.close();
// and we're finished
return SUCCESS;
To wrap around a stream from the iostream library, reading data until the end of the stream:
#include <fstream>
#include <binwrap.h>
// Open the iostream stream for reading and writing in binary mode.
fstream file("test.dat", ios::in | ios::out | ios::bin);
// Wrap a binary input-only stream around it.
biniwstream bfile(&file);
// Read (and immediately discard) 24-bit integers until the end
// of the stream.
while(!bfile.eof())
bfile.readInt(3);