write_array
write_array(path, data, [type="f64"], [endian="little"])
Input: path, data, type, endian · Output: no output
What it is
Writes a whole Vec/Mat/Signal (or a bare number/bool) to a binary file in one call, without an explicit fopen — the write-side counterpart read_array never had. path (string) is the destination, overwritten if it exists. data is the value to write; a Mat is flattened in its own column-major storage order (the same order A -> (r, c) fills), not row-major — reading it back with read_array and reshaping recovers the original matrix, since a flat binary file carries no shape of its own. type/endian as in read_array.
Overloads
One call shape — this function is not overloaded.
| Code | Input | Output | Description |
write_array(path, data, [type="f64"], [endian="little"]) | path stringpath (string) is the destination, overwritten if it exists.data —data is the value to write.type optional (default "f64")type/endian as in read_array.endian optional (default "little")type/endian as in read_array. | no output | Writes a whole Vec/Mat/Signal (or a bare number/bool) to a binary file in one call, without an explicit fopen — the write-side counterpart read_array never had. |
Examples
From the documentation, and run by the build -- the output and the figure below are what the engine actually produced. Simplest first.
# The file is written here first. Mapping one the repository does not
# carry would make this block fail, and a block that fails is dropped
# from the page while the build still reports success.
write_array("big_dataset.bin", 0 to 511, type = "u8")
m = mmap_open("big_dataset.bin")
n = mmap_len(m)
header = mmap_read(m, 0, 16) # first 16 bytes, wherever they live
record5 = mmap_read(m, 5 * 64, 64) # the 6th fixed-width 64-byte record
print(n)
print(header)
print(record5)
512 [0, 1, 2, 3, 4, 5, 6, 7, ... (16 elements)] [255, 255, 255, 255, 255, 255, 255, 255, ... (64 elements)]
from file-io
f = fopen("sample64.bin", "wb")
write_int64(f, -123456789012)
write_uint64(f, 9007199254740993) # 1 past 2^53 -- the exact-precision edge
close(f)
g = fopen("sample64.bin", "rb")
a = read_int64(g)
b = read_uint64(g)
close(g)
print("{a} {b}") # -123456789012 9007199254740992 -- rounded
M = [1, 2, 3; 4, 5, 6]
write_array("matrix.bin", M, type = "int32")
back = read_array("matrix.bin", type = "int32") -> (2, 3)
print(back) # [1, 2, 3; 4, 5, 6] -- shape recovered via reshape
-123456789012 9007199254740992 [1, 2, 3; 4, 5, 6]
from file-io