Quirrel4.41.0

io.file.len

Method of file, from module "io"
instance.len(): int

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

examples/io/file/len.nut
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) = 5

See also

lenReturns the stream length
tellReturns the current stream position
eosReturns non-null if the stream is at end-of-stream
seekSeeks to the given offset; origin is 'b' (begin), 'c' (current) or 'e' (end)
fileclass index