File.open
func File.open(path: string): Result[File.Handle, Error]File.open opens the file at path for reading. Close it with File.close. A directory opens, and a read of it is Err.
Details
- A write to the handle is
Err; open a file for writing with File.create or File.append. File.opennever creates a file. A missing file isErr.
Basic examples
Open a file and read it line by line, closing it when the script ends:
let f = try File.open("names.txt")
defer { f.close() }
while true {
let line = try f.readLine() ?? break
println("Hello, ${line}")
}Hello, Ann
Hello, Bob
Hello, CyA missing file is Err:
println(File.open("missing.txt"))Err{ error: Error{ message: "open missing.txt: no such file or directory" } }Scope
Reads continue where the last one stopped, whatever their kind. Read a header line, then the rest:
let f = try File.open("data.txt")
defer { f.close() }
println(try f.readLine())
print(try f.readAll())id,name
1,ant
2,beeThe bytes of a file come back as they are through File.readChunk:
let f = try File.open("magic.bin")
defer { f.close() }
println(try f.readChunk(3))
println(try f.readExactly(3))Bytes(47 49 46)
Bytes(38 39 61)An empty file opens, and its first line read is null:
let f = try File.open("empty.txt")
defer { f.close() }
println(f.readLine())Ok{ value: null }Applications
Count the lines of a file without holding it in memory, and say what failed in the error:
func countLines(path: string): Result[int, Error] {
let f = try (File.open(path) | Result.wrapErr("count lines"))
defer { f.close() }
var n = 0
while (try f.readLine()) != null {
n += 1
}
Result.ok(n)
}
println(countLines("log.txt"))
println(countLines("nope.txt"))Ok{ value: 3 }
Err{ error: Error{ message: "count lines: open nope.txt: no such file or directory" } }Copy a file to standard output, as cat does, with File.pipe:
let f = try File.open("poem.txt")
defer { f.close() }
try File.pipe(f, Os.stdout())Roses are redFind the first line that matches, and stop reading there:
func findLine(path: string, prefix: string): Result[string | null, Error] {
let f = try File.open(path)
defer { f.close() }
while true {
let line = try f.readLine() ?? break
if line.startsWith(prefix) { return Result.ok(line) }
}
Result.ok(null)
}
println(findLine("config.txt", "port="))
println(findLine("config.txt", "user="))Ok{ value: "port=8080" }
Ok{ value: null }Properties and relations
Reading a whole handle with File.readAll gives what File.read gives:
let f = try File.open("text.txt")
defer { f.close() }
assert(try f.readAll() == try File.read("text.txt"))Each File.open gives a new handle that reads from the start of the file:
let a = try File.open("abc.txt")
defer { a.close() }
let b = try File.open("abc.txt")
defer { b.close() }
assert(a != b)
assert(try a.readChunk(1) == try b.readChunk(1))Possible issues
The handle cannot be written. Open a file for writing with File.create or File.append:
let f = try File.open("notes.txt")
defer { f.close() }
println(f.writeLine("more"))Err{ error: Error{ message: "the file is not open for writing" } }See also
File.close · File.read · File.readLine · File.readAll · File.create · File.append · File.lock · File.list
