SultanC
  1. Home
  2. Examples
  3. Files

Writing a file, reading it back, then removing it

fs/files.sn
// Writing a file, reading it back, then removing it.
import std.io.println;
import std.fs.write_file;
import std.fs.read_file;
import std.fs.mutation;

def main(): int {
    let path: string = "sultanc-example.tmp";
    let content: string = "saved by SultanC";
    match write_file(&path, &content) with
    | result.err(code) -> { println("write failed"); return code; }
    | result.ok(_) -> { };
    match read_file(&path) with
    | result.ok(text) -> { println(text); }
    | result.err(code) -> { println("read failed"); return code; };
    match remove_file(&path) with
    | result.ok(_) -> { println("removed"); }
    | result.err(code) -> { return code; };
    return 0;
}

Run it

With the interpreter, without compiling:

./build/sultanc run examples/en/fs/files.sn

Or compile it to an executable:

./build/sultanc examples/en/fs/files.sn -o build/example && ./build/example

Source on GitHub   All examples