#include <oxstd.h>

main()
{
    decl file, x, xtx, i, ct, in;
    decl cfbsize, cfcols, cfblock, cfrows;

    file = fopen("iotest11.bin", "wb");   // write

    cfcols = 4;               // number of columns
    cfblock = 10;              // number of blocks
    cfbsize = 100;                   // block size

    fwrite(file, cfbsize * cfblock);//write # rows
    fwrite(file, cfcols);           //write # cols
    fwrite(file, cfbsize);

    for (i = 0; i < cfblock; ++i)  // write matrix
        fwrite(file, rann(cfbsize, cfcols));

    fclose(file);

    cfcols = cfblock = cfbsize = 0;

    file = fopen("iotest11.bin", "rb");    // read

    fread(file, &cfrows , 'i');
    fread(file, &cfcols , 'i');
    fread(file, &cfbsize, 'i');

    for (xtx = 0.0, ct = 0; (in = fread(file, &x, 'f', cfbsize, cfcols)) > 0; )
    {
        xtx += x'x;
        ct += idiv(in, cfcols);
    }
    fclose(file);
    if (ct == cfrows)
        print("file OK\n");
    else
        print("file **not** OK\n");


    print("X\'X from file (", ct, " rows):", xtx);

    ranseed(-1);
    x = rann(cfrows, cfcols);
    print("X\'X:", x'x);

}

