Qu

read_array

Builtin index · File I/O

result = read_array(path, [type="f64"], [endian="little"])

Input: path, type, endian · Output: result

What it is

Reads a whole binary file as one typed array, without an explicit fopen. path (string) is the file. type (string, optional, default "f64", same type names as read_values) is the element type. endian (string, optional, default "little", or "big") is the byte order.

Overloads

One call shape — this function is not overloaded.

CodeInputOutputDescription
result = read_array(path, [type="f64"], [endian="little"])
path stringpath (string) is the file.
type string, optional (default "f64")type (string, optional, default "f64", same type names as read_values) is the element type.
endian string, optional (default "little")endian (string, optional, default "little", or "big") is the byte order.
result Vec or number
Returns a Vec of numbers — one per element in the file. Errors if the file's byte length isn't a whole multiple of the element width, rather than silently truncating a partial trailing element.

Examples

From the documentation, and run by the build -- the output and the figure below are what the engine actually produced. Simplest first.

f = fopen("temps.bin", "wb")
write_double(f, 20.5)
write_double(f, 21.0)
write_double(f, 19.8)
close(f)

print(file_size("temps.bin"))            # 24 -- 3 doubles, 8 bytes each


g = fopen("temps.bin", "rb")
vals = read_values(g, "f64", 3)
close(g)
print(vals)                              # [20.5, 21, 19.8]


whole = read_array("temps.bin", type="f64")
print(whole)                             # [20.5, 21, 19.8]

output
24
[20.5, 21, 19.8]
[20.5, 21, 19.8]

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

output
-123456789012 9007199254740992
[1, 2, 3; 4, 5, 6]

from file-io