Skip to main content

File module (file)

Start here

The file module reads and writes disk paths from Flow-Wing. You bring file. When an operation can fail, you get Err::Result on the result side, so in real code you also bring Err and use isErr() (or Err::isErr) to branch on failures.

Two styles:

  • All at oncefile::readText, file::writeText, file::appendText (good for small files).
  • Open, then read/writefile::File for open, readLine, close, when you work like a small script with a file handle.

Script location: file::__DIR__ is a string path to the current .fg file's directory (handy for loading assets next to the program).

Example (read and write, with errors)

bring file
bring Err

var path: str = "example.txt"
var _, werr: Err::Result = file::writeText(path, "Hello\n")
if werr.isErr() {
println(werr.getMessage())
} else {
var content: str, rerr: Err::Result = file::readText(path)
if rerr.isErr() {
  println(rerr.getMessage())
} else {
  println(content)
}
}

Stateful file::File (open, readLine, close)

bring file
bring Err

var f: file::File = new file::File("example.txt")
var err: Err::Result = f.open("r")
if err.isErr() {
println(err.getMessage())
} else {
var line: str, rerr: Err::Result = f.readLine()
if rerr.isErr() {
  println(rerr.getMessage())
} else {
  println(line)
}
f.close()
}

Whole-file helpers (summary)

  • file::readText(path: str) -> str, Err::Result
  • file::writeText(path: str, content: str) -> bool, Err::Result
  • file::appendText(path: str, content: str) -> bool, Err::Result
  • file::exists(path: str) -> bool
  • file::delete(path: str) -> bool, Err::Result

More detail: errors and paths

Always treat I/O as fallible: missing files, permission errors, and invalid paths show up on Err::Result. Use the Err module's patterns in Built-in libraries → Err module to report messages and codes. Path strings follow your OS rules; on Windows, backslashes in string literals and string building are normal in .fg programs.

Source & tests (if you have the repository)

WhatWhere
Module source (reference)fw-modules/file_module/file-module.fg.
Integration-style fixturestests/fixtures/LatestTests/FileModuleTests/ — reads, writes, and __DIR__ behavior in the test harness.