File

#import Builtin.File;

File I/O - open, read, write, and close files on disk. Wraps the C standard library file functions.

Example:

#import Builtin.File;

f : File = open("hello.txt", "w");
writeLine(f, "Hello, world!");
close(f);

g : File = open("hello.txt", "r");
contents : string = readAll(g);
close(g);

Functions


openFile(path, mode)

openFile :: File(path : string, mode : string)

Opens a file and returns a File handle. Mode follows C conventions: "r" (read), "w" (write/create), "a" (append), "r+" (read+write), "w+" (create+read+write). Check .isOpen to confirm the file opened successfully.

closeFile(f)

closeFile :: (f : File)

Closes the file and releases the handle.

readAll(f)

readAll :: string(f : File)

Reads the entire contents of the file and returns it as a string.

readLine(f)

readLine :: string(f : File)

Reads one line from the file (up to 4095 characters). Returns an empty string at end of file.

atEnd(f)

atEnd :: bool(f : File)

Returns true if the file position is at end of file.

writeFile(f, s)

writeFile :: (f : File, s : string)

Writes a string to the file.

writeLine(f, s)

writeLine :: (f : File, s : string)

Writes a string followed by a newline to the file.

deleteFile(path)

deleteFile :: bool(path : string)

Deletes the file at the given path. Returns true on success.

Structs


File

A handle to an open file on disk.