read_line
result = read_line(f)
Input: f · Output: result
What it is
Reads one line from f (file handle, opened readable).
Overloads
One call shape — this function is not overloaded.
| Code | Input | Output | Description |
result = read_line(f) | f stringnot documented yet | result string | Returns a string holding the line's content without its trailing newline (\r\n or \n), or Nothing once there is no more content to read. |
Examples
From the documentation, and run by the build -- the output and the figure below are what the engine actually produced. Simplest first.
f = fopen("log.txt", "r")
for i = 0 to 1
print(read_line(f))
end for
close(f)
g = fopen("log.txt", "r")
print(read_all(g))
close(g)
output
run started value = 42 run started value = 42
from file-io
ports = serial_ports() # e.g. ["COM3"], or [] with no hardware attached
conn = serial_open(ports[0], 9600, timeout_ms=500)
line = read_line(conn) # blocks up to 500ms; `none` if nothing arrived
if line != none
print("got: {line}")
else
print("no line arrived within the timeout")
end if
close(conn)
output
no line arrived within the timeout
from concurrency
# 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
ports = serial_ports()
print("found {length(ports)} port(s): {ports}")
conn = serial_open(ports[0], 115200, timeout_ms = 1000)
write(conn, "MEAS?\n")
line = read_line(conn) ?? "(timed out)"
print(line)
close(conn)
not run by the build — this one needs something the repository cannot supply · from specialized