Home Documentation Templates Examples Showcase GitHub
Theme

Scripting · File streams

File streams.

Use open(path) for convenient whole-file text, file(path) for managed transactional editing, and ifs(path)/ofs(path) when data should be consumed or produced incrementally.

Input streams

stream := ifs("data.txt")

line := stream.read_line()
rest := stream.read_all()

close(stream)
MethodResult
read(n)Read up to n bytes from the current position.
read_line()Read the next line.
read_all()Read the remaining stream contents.
read_val()Parse the next serialized Nift data value using Nift's value parser.
eof()Report the stream's EOF state.
values := ifs("values.txt")
flag := values.read_val()
count := values.read_val()
items := values.read_val()

read_val() understands supported serialized Nift data values such as null, booleans, integers, doubles, strings and arrays/data values. Malformed input is an error rather than silently becoming a string.

Output streams

out := ofs("result.txt")
out.write("hello")
out.write_line(" world")
out.flush()
close(out)

ofs(path) creates/truncates its output file. Streams are resource values with identity and closed-state checks; they are not directly renderable as template/console values.