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 = File("hello.txt", "w");
writeLine(f, "Hello, world!");
close(f);

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

Functions


File()

create :: File()

Creates an empty file object

Returns:

An empty File object

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.

Methods:

File.open(path, mode)

open :: File(path : const string, mode : const 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.

File.close()

close :: ()

Closes the file and releases the handle.

File.readAll()

readAll :: string()

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

File.readLine()

readLine :: string()

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

File.atEnd()

atEnd :: bool()

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

File.write(s)

write :: (s : string)

Writes a string to the file.

File.writeLine(s)

writeLine :: (s : string)

Writes a string followed by a newline to the file.