Files

Read and write files in Caboose.

Opening Files

Opening files in Caboose is simple. To open a file, use the with statement. The syntax of the with statement is: with(<filename>, "<mode>") {} where <filename> is a string and <mode> is a proper open mode.

with("filename.txt", "r") {
// `file` variable passed here.
println(file.read());
}

Open Modes

Open Mode

Description

r

Opens a file for reading, the file must exist already.

w

Opens a file for writing, if a file does not exist one is created, else existing file is overwritten

a

Opens a file for appending, if a file does not exist one is created, else appends text to the end of a file

File Methods

.read()

Read all lines of a file into a string.

Signature:

string file.read()