io.file.len
Returns the stream length
Return value
See iostream.stream.len for the generic contract.
Errors
Throws the stream is invalid once the file has been closed.
Notes
A file has no stored length field: every call seeks to the real end, reads the position, and seeks back to where the cursor was, so len costs two extra seeks. Prefer keeping a running total in script code over calling len in a loop.
len does not move, and is not moved by, the cursor: seeking past the real end (see seek) changes tell without changing len, since nothing has been written out there yet.
Example
from "io" import file
from "system" import getenv, remove
let path = $"{getenv("TEMP") ?? getenv("TMP") ?? "/tmp"}/qrl_io_file_len.tmp"
let f = file(path, "w+")
f.writestring("abcde")
println("f.len() after write =", f.len()) // total size on disk
f.seek(2)
println("f.len() after seek(2) =", f.len()) // len does not depend on the cursor, unlike tell
f.close()
remove(path)Output:
f.len() after write = 5
f.len() after seek(2) = 5See also
len | Returns the stream length |
tell | Returns the current stream position |
eos | Returns non-null if the stream is at end-of-stream |
seek | Seeks to the given offset; origin is 'b' (begin), 'c' (current) or 'e' (end) |
| file | class index |