io
Reading from the keyboard, reading and writing files, and writing to standard error.
import io
Most programs have to read something and write something. The io package holds the simplest ways to do it: read one line from the user, read or write a whole file in one go, or work through an open file piece by piece.
The functions that take a file name, such as ReadFile, open and close the file themselves. The functions that take a File* work on a file you opened yourself with os.Open; remember to close it with Close.
Sizes in this package are counted in bytes, not characters. Outside ASCII one character takes several bytes, so reading a file in chunks can split a character across two chunks.
Functions
Input
func Input(): strinput "" does. The example below waits for you to type, so here it is only compiled and not run.import io
func main:
print "What is your name? "
name := io.Input()
println "Welcome,", name
endRead
func Read(f: File*, size: u64): strsize bytes from the current position in the file. At the end of the file it returns an empty string.import io
import os
func main:
io.WriteFile("data.txt", "salam\ndonya\npayan\n")
f := os.Open("data.txt", "rb")
println io.Readline(f)
println io.Read(f, 3 as u64)
io.Seek(f, 0, 0)
print io.ReadAll(f)
io.Close(f)
endReadline
func Readline(f: File*): strThe example for this item is shown with the ones above: io-Read.salam
Write
func Write(f: File*, data: str): intimport io
import os
func main:
f := os.Open("report.txt", "wb")
n := io.Write(f, "today's report\n")
io.Close(f)
println "bytes written:", n
src := os.Open("report.txt", "rb")
dst := os.Open("copy.txt", "wb")
println "copied:", io.Copy(dst, src), "bytes"
io.Close(src)
io.Close(dst)
print io.ReadFile("copy.txt")
endSeek
func Seek(f: File*, offset: i64, whence: int): intoffset is a distance in bytes and whence says where to count from: zero is the start of the file, one is the current position and two is the end. It returns zero on success.The example for this item is shown with the ones above: io-Read.salam
Close
func Close(f: File*)defer is a good place for it.The example for this item is shown with the ones above: io-Read.salam
ReadFile
func ReadFile(path: str): strimport io
func main:
path := "note.txt"
io.WriteFile(path, "first line\n")
io.AppendFile(path, "second line\n")
print io.ReadFile(path)
println io.ReadFile("a file that is not there.txt") == ""
endWriteFile
func WriteFile(path: str, data: str): intThe example for this item is shown with the ones above: io-ReadFile.salam
AppendFile
func AppendFile(path: str, data: str): intWriteFile it returns a byte count or minus one.The example for this item is shown with the ones above: io-ReadFile.salam
Lines
func Lines(path: str): Vector<str>free when you are done with it.import io
func main:
mut lines := Vector {} as Vector<str>
defer lines.free()
lines.push("apple")
lines.push("pomegranate")
lines.push("grape")
written := io.WriteLines("fruit.txt", lines)
println "wrote:", written, "bytes"
read := io.Lines("fruit.txt")
defer read.free()
each i, line in read:
println i + 1, line
end
endEPrint
func EPrint(s: str)import io
func main:
println "this goes to standard output"
io.EPrint("warning: ")
io.EPrintln("this message goes to standard error")
print "no line break yet..."
io.Flush()
println ""
endEPrintln
func EPrintln(s: str)EPrint, but moves to the next line after the message.The example for this item is shown with the ones above: io-EPrint.salam
Flush
func Flush()print that has no line ending and want the user to see it at once.The example for this item is shown with the ones above: io-EPrint.salam
ReadAll
func ReadAll(f: File*): strThe example for this item is shown with the ones above: io-Read.salam
Copy
func Copy(dst: File*, src: File*): i64src into the file dst and returns the number of bytes copied. It suits large files, since it never holds the whole file in memory.The example for this item is shown with the ones above: io-Write.salam
WriteLines
func WriteLines(path: str, lines: Vector<str>): intThe example for this item is shown with the ones above: io-Lines.salam