Qu

peek_line

Builtin index · File I/O

result = peek_line(f)

Input: f · Output: result

What it is

Looks at the next line of f (file handle, opened readable) without consuming it.

Overloads

One call shape — this function is not overloaded.

CodeInputOutputDescription
result = peek_line(f)
f stringnot documented yet
result string
Returns exactly what read_line(f) would return (a string, or Nothing at EOF), but leaves the read cursor exactly where it was.

Examples

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

# 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

How it works

peek_line/peek_char/peek_byte (§ file I/O extensions, wrappers around read_line/read_char/read_byte; see fs_ops::call's own doc comment for the full design (why three distinct names rather than one generic peek(), and why this is implemented via a real seek round-trip rather than a buffered "un-read").

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