File.writeLine
func File.writeLine(f: File.Handle, line: string): Result[void, Error]File.writeLine writes line and a newline to f.
Details
- A
\ninsidelineis written as it is, so the text reads back as more than one line. - The write reaches the OS before
File.writeLinereturns, with all of its bytes orErr. Only a handle from File.buffered holds writes back. - A handle from File.open, or one already closed, is
Err.
Basic examples
Write a line to a file, then close it:
let out = try File.create("todo.txt")
defer { out.close() }
try File.writeLine(out, "buy milk")
try out.close()
println(File.read("todo.txt"))Ok{ value: "buy milk\n" }Write a message to standard error:
try File.writeLine(Os.stderr(), "warning: disk almost full")
println("done")doneScope
An empty line is a newline alone, and Unicode text is written as UTF-8:
let out = try File.create("lines.txt")
defer { out.close() }
for line in ["one", "", "héllo 👋"] {
try out.writeLine(line)
}
try out.close()
println(File.read("lines.txt"))Ok{ value: "one\n\nhéllo 👋\n" }A handle from File.append adds lines at the end of what the file holds:
let log = try File.append("app.log")
defer { log.close() }
try log.writeLine("stopped")
try log.close()
print(try File.read("app.log"))started
stoppedA connection is a handle too. Here a client sends a line over Net, and the server reads it with File.readLine:
let listener = try Net.listen("127.0.0.1:0")
let client = try Net.dial(listener.address())
defer { client.close() }
let server = try Net.accept(listener)
defer { server.close() }
try client.writeLine("ping")
println(server.readLine())Ok{ value: "ping" }Applications
Write records as CSV-like rows, one line each, and report a failed close:
type Score = { name: string, points: int }
func saveScores(path: string, scores: Array[Score]): Result[void, Error] {
let out = try (File.create(path) | Result.wrapErr("scores"))
defer { out.close() }
try out.writeLine("name,points")
for s in scores {
try out.writeLine("${s.name},${s.points}")
}
out.close()
}
println(saveScores("scores.csv", [Score{ name: "Ann", points: 9 }, Score{ name: "Bob", points: 7 }]))
print(try File.read("scores.csv"))Ok{ value: void }
name,points
Ann,9
Bob,7Buffer many small writes with File.buffered, and close the buffered handle to write them out:
let out = File.buffered(Os.stdout())
for i in Iter.range(1, 4) {
try out.writeLine("line ${i}")
}
try out.close()line 1
line 2
line 3Properties and relations
File.writeLine(f, line) writes what File.writeText(f, line + "\n") writes:
let a = try File.create("a.txt")
defer { a.close() }
let b = try File.create("b.txt")
defer { b.close() }
for line in ["x", "", "y z"] {
try a.writeLine(line)
try b.writeText(line + "\n")
}
try a.close()
try b.close()
assert(File.read("a.txt") == File.read("b.txt"))Lines with no \n and no final \r read back as written, through String.lines:
let lines = ["alpha", "", "gamma"]
let out = try File.create("round.txt")
defer { out.close() }
for line in lines {
try out.writeLine(line)
}
try out.close()
assert((try File.read("round.txt")).lines() == lines)Possible issues
A \n inside the line splits it, so it reads back as two lines:
let out = try File.create("split.txt")
defer { out.close() }
try out.writeLine("first\nsecond")
try out.close()
println((try File.read("split.txt")).lines())["first", "second"]A handle from File.open is for reading, and a closed handle takes no more writes:
let input = try File.open("notes.txt")
defer { input.close() }
println(input.writeLine("more"))
let out = try File.create("out.txt")
try out.close()
println(out.writeLine("late"))Err{ error: Error{ message: "the file is not open for writing" } }
Err{ error: Error{ message: "the file is closed" } }Lines written to a buffered handle are not in the file until the buffer fills, or until a flush or a close:
let out = File.buffered(try File.create("held.txt"))
defer { out.close() }
try out.writeLine("held")
println(File.read("held.txt"))
try out.flush()
println(File.read("held.txt"))Ok{ value: "" }
Ok{ value: "held\n" }A write to standard output or standard error whose reader has gone, as when the output is piped to head, ends the program with status 141, as SIGPIPE does. It does not return Err. Where no signal comes, as on Windows, it returns Err.
The line must be a string. Convert other values first, with String.from or interpolation:
try File.writeLine(Os.stdout(), 42) // error: cannot use int where string is expectedSee also
File.writeText · File.writeChunk · File.readLine · File.create · File.append · File.buffered · File.close · println · Os.stderr
