Files
Files reads and writes whole files in one call.
#import "Files.xc"Overview
Section titled “Overview”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.
Methods
Section titled “Methods”readText
Section titled “readText”static String* readText(String* path)The whole file as a string, or 0 when it cannot be read.
readData
Section titled “readData”static Data* readData(String* path)The whole file as bytes, or 0 when it cannot be read.
writeText
Section titled “writeText”static bool writeText(String* path, String* text)Replaces the file with text, creating it if needed.
writeData
Section titled “writeData”static bool writeData(String* path, Data* data)Replaces the file with data, creating it if needed.
appendText
Section titled “appendText”static bool appendText(String* path, String* text)Adds text to the end of the file, creating it if needed.
setExecutable
Section titled “setExecutable”static bool setExecutable(String* path)Marks the file executable (mode 0755).
createDirectory
Section titled “createDirectory”static bool createDirectory(String* path)Creates a directory. One that already exists counts as success.
exists
Section titled “exists”static bool exists(String* path)Whether the path exists. On macOS this ignores case, as the filesystem does.
existsExact
Section titled “existsExact”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.