Qu

write_line

Builtin index · File I/O

write_line(f, s)

Input: f, s · Output: no output

What it is

Writes s (string) to f (file handle, opened writable/appendable) plus a trailing \n.

Overloads

One call shape — this function is not overloaded.

CodeInputOutputDescription
write_line(f, s)
f stringnot documented yet
s stringnot documented yet
no output—

Examples

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

n = fopen("notes.txt", "w")
write_line(n, "line one")
write_line(n, "line two")
close(n)
print(read_all_text("notes.txt"))
# line one

# line two

output
line one
line two

from file-io

f = fopen("log.txt", "w")
write_line(f, "run started")
write_line(f, "value = " + str(42))
close(f)
print(file_size("log.txt"))    # 23 -- "run started\n" + "value = 42\n"

output
23

from file-io

# The three-line file this reads is written here. It used to open a

# log.txt that the chapter does not create until 700 lines further down,

# so this block failed and was dropped from the page while the build

# reported success.

w = fopen("log.txt", "w")
write_line(w, "a")
write_line(w, "b")
write_line(w, "c")
close(w)

f = fopen("log.txt", "r")
first_line = peek_line(f)          # doesn't consume anything

same_line  = read_line(f)          # sees first_line again

next_line  = read_line(f)          # only NOW does this move on

print("{first_line} | {same_line} | {next_line}")
close(f)
# a | a | b

output
a | a | b

from file-io

f = fopen("streaming_demo.txt", "w")
for i = 0 to 4
    write_line(f, "line " + str(i))
end for
close(f)

g = fopen("streaming_demo.txt", "r")
n = 0
while not eof(g)
    line = read_line(g)
    n = n + 1
end while
close(g)
print("lines read = {n}")

from tour

How it works

write(f, s) (file form, above) — raw string/bytes, no added newline. write_line(f, s) — the same, plus a trailing \n. Writing to a handle opened in "r" mode errors clearly (file_check_writable), never silently no-ops or panics.

From the note above this builtin in the interpreter, where the reasoning was written down as it was built.