#include <oxstd.h>

main()
{
    decl file, r, c, rowsize, headersize, s, x;

    print(loadmat("iotest12.dht"));


    file = fopen("iotest12.dht", "rbe");
    fread(file, &s, 'c', 2);

    if (s == "\xDA\xEE")
    {
        print("signature OK, is 32 bit .DHT/.DAT file\n");

        fread(file, &s, 'c', 2);        // data type 2 byte integer
        if (s[0] != 8)
        {   print("can only handle 8 byte doubles\n");
            exit(1);
        }
        fread(file, &s, 'c', 4);        // skip 4 bytes
        fread(file, &c, 'i');           // columns: 4 byte integer
        fread(file, &rowsize, 'i');     // row size: 4 byte integer
        fread(file, &headersize, 'i');  // header size: 2 byte integer
    }
    else if (s == "\xDA\xDA")
    {
        print("signature OK, is 16 bit .DHT/.DAT file\n");

        fread(file, &s, 'c', 4);        // skip 4 bytes
        fread(file, &s, 'c', 2);        // columns: 2 byte integer
        c = s[1] * 256 + s[0];
        fread(file, &s, 'c', 2);        // row size: 2 byte integer
        rowsize = s[1] * 256 + s[0];
        fread(file, &s, 'c', 2);        // header size: 2 byte integer
        headersize = s[1] * 256 + s[0];
        fread(file, &s, 'c', 2);        // data type 2 byte integer
        if (s[0] != 8)
        {   print("can only handle 8 byte doubles\n");
//          exit(1);
        }
    }
    else
    {   print("signature NOT OK!\n");
        exit(1);
    }
    fclose(file);
    print("rowsize=", rowsize, " columns=", c, "\n");

    file = fopen("iotest12.dat", "rbe");// open data file
    fseek(file, 'f', c);                // skip first row

    for ( ;fread(file, &x, 'f', 1, c) > 0; )
    {
        print(x);
    }
    fclose(file);
}
