How could I parse binary data files?

How could I parse binary data files?


[user01@centos66 binaryTest]$ od -bc int32.bin

0000000 000 000 000 000 001 000 000 000 002 000 000 000 003 000 000 000

\0 \0 \0 \0 001 \0 \0 \0 002 \0 \0 \0 003 \0 \0 \0

0000020

[user01@centos66 binaryTest]$ od -bc double.bin

0000000 000 000 000 000 000 000 000 000 000 000 000 000 000 000 360 077

\0 \0 \0 \0 \0 \0 \0 \0 \0 \0 \0 \0 \0 \0 360 ?

0000020 000 000 000 000 000 000 000 100 000 000 000 000 000 000 010 100

\0 \0 \0 \0 \0 \0 \0 @ \0 \0 \0 \0 \0 \0 \b @

0000040


[user01@centos66 binaryTest]$ cat a.cpp

#include <iostream>

#include <fstream>

using namespace std;


int main() {

ofstream ofsInt32(“int32.bin”, ios::binary);

for(int a = 0; a < 4; ++a) {

ofsInt32.write(reinterpret_cast<const char *>(&a), sizeof(a));

}

ofsInt32.close();


ofstream ofsDouble(“double.bin”, ios::binary);

for(double a = 0; a < 4; ++a) {

ofsDouble.write(reinterpret_cast<const char *>(&a), sizeof(a));

}

ofsDouble.close();

return 0;

}

Hi,

You should be able to use the 1: (one colon) function to read binary files. It requires a list of
types and the widths of those types in order to parse the data correctly. The examples below are
from running your sample code on a 64 bit Linux system. You will need to take care with the widths
and byte ordering on different systems.

    / Reading the four integers from the binary file in little endian format
    q) (“iiii”, 4 4 4 4)1:`int32.bin
    0
    1
    2
    3

    / Reading the four doubles from the binary file in little endian format
    q) (“ffff”; 8 8 8 8)1:`double.bin
    0
    1
    2
    3

More information on this operator can be found at:

http://code.kx.com/wiki/Reference/OneColon

Thanks

Mark Rooney
Financial Software Developer
AQUAQ Analytics

thx ^_^