std.fil
std.fil is the simplest layer between your program and the file system: check whether a file exists, read the whole content as text, write or append, and read and write raw bytes.
std.fil is the simplest layer between your program and the file system. You can check whether a file exists, read the whole content as text, write or append, and for binary content read and write raw bytes.
All of these operations require the right disk capabilities — disk.read to read, disk.write to write — limited to the directories you name when you start the program. That is on purpose: a program without them cannot touch the file system by accident, and a dependency you have not read cannot read files it was never granted access to.
When to use it
Use std.fil to read configuration, save results or process files a user has left behind. The text functions cover the ordinary cases; the byte functions handle images and other binary content. Everything requires the right disk capabilities.
Getting started
bruk std.fil som fil
funksjon start() -> heiltall {
hvis fil.finnes("notat.txt") {
skriv(fil.les("notat.txt"))
} ellers {
fil.skriv_fil("notat.txt", "Første linje\n")
}
fil.append("notat.txt", "En linje til\n")
returner 0
}finnes lets you check before you read, and append adds without overwriting. Remember disk.read/disk.write when you start the program.
The functions
Think of the functions in three layers: check (finnes, status), text (les, les_fil, skriv_fil, append) and bytes for binary content (les_bytes, les_byte_liste, skriv_bytes). slett_fil removes a file.
finnes(sti: tekst) -> bool— True if the file exists at the path.skriv_fil(sti: tekst, innhald: tekst) -> heltall— Writes text to the file, overwriting any previous content.les_fil(sti: tekst) -> tekst— Reads and returns the whole content of the file as text.les(sti: tekst) -> tekst— Reads the whole file as text (short form ofles_fil).append(sti: tekst, innhald: tekst) -> heltall— Adds text to the end of the file without overwriting.skriv_bytes(sti: tekst, data) -> heltall— Writes binary data (a byte buffer) to the file.les_bytes(sti: tekst)— Reads the file as binary data.les_byte_liste(sti: tekst) -> liste_heltall— Reads the file as aliste_heltallof byte values.slett_fil(sti: tekst) -> bool— Deletes the file and returns true if it succeeded.status(sti: tekst) -> ordbok_tekst— Returns metadata about the file, such as whether it exists and how big it is.
Good to know
- Without the
disk.read/disk.writecapability the operations fail withmanglar capability— that is not a bug in the code, but the runtime doing its job. skriv_filoverwrites; to keep what is there, useappend.- For images and other binary content, use
les_bytes/skriv_bytes— the text functions are not safe on raw bytes.
std.fil is one of the modules in the standard library. Everything ships with the runtime — no installation, no external dependencies. See also Documentation for the language and the runtime.
Related
- std.tekststd.tekst is the toolbox for everything to do with text — searching and checking, splitting and joining, trimming whitespace, changing case, and converting to and from numbers.
- std.httpstd.http is a client for talking to other services over HTTP and HTTPS. You can fetch data, send data, and interpret the response — including JSON — without building the request by hand.
- std.dbstd.db gives you access to databases with a small, familiar set of functions: open a connection, run SQL, fetch values and rows, and control transactions.