Skip to content

Files

Files reads and writes whole files in one call.

#import "Files.xc"
String* src = Files.readText(String.withCString("prog.xc"));
if (src == 0) { /* missing or unreadable */ }

A file that cannot be read is 0, and an empty file is an empty string, so “missing” and “empty” stay different answers. A write is true only when every byte was written.

The class is Files, not File: macOS filesystems ignore case, so a File.xc would be the same file as FILE.xc, the console streams.

AsyncFiles runs the same operations on a background thread.

static String* readText(String* path)

The whole file as a string, or 0 when it cannot be read.

static Data* readData(String* path)

The whole file as bytes, or 0 when it cannot be read.

static bool writeText(String* path, String* text)

Replaces the file with text, creating it if needed.

static bool writeData(String* path, Data* data)

Replaces the file with data, creating it if needed.

static bool appendText(String* path, String* text)

Adds text to the end of the file, creating it if needed.

static bool setExecutable(String* path)

Marks the file executable (mode 0755).

static bool createDirectory(String* path)

Creates a directory. One that already exists counts as success.

static bool exists(String* path)

Whether the path exists. On macOS this ignores case, as the filesystem does.

static bool existsExact(String* path)

Whether the path exists with exactly this spelling, case included. Use it when resolving a name a person typed, so Sort.xc does not quietly open sort.xc.

static i32 size(String* path)

The file’s size in bytes, or -1 when it cannot be opened.