#include <oxstd.h>

main()
{
    decl file, x, xtx, i, ct, in;

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

    for (i = 0; i < 10; ++i)     // write 1000 x 4
        fwrite(file, rann(100, 4));      // matrix

    fclose(file);

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

    //  assumes we know that number of columns = 4
    // perhaps a better way is to write the number
    // number of columns in the file, see iotest11
    for (xtx = 0.0, ct = 0; (in = fread(file, &x, 'f', 150, 4)) > 0; )
    {
        xtx += x'x;
        ct += idiv(in, 4);
    }
    fclose(file);

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

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

}
