The std/io module provides comprehensive file I/O operations including handle-based streaming and convenience functions for reading and writing files.
Import
File handles
The std/io module uses integer file handles for streaming operations. Handles must be opened with file_open() and closed with file_close() when finished.
File modes
| Mode | Description |
|---|
"r" | Read-only mode |
"w" | Write mode (creates or truncates file) |
"a" | Append mode (creates if needed) |
"rw" | Read-write mode (creates if needed) |
Functions
file_open
Open a file and return a handle for streaming operations.
let handle = io.file_open(path, mode);
File mode: "r", "w", "a", or "rw"
File handle for subsequent operations
Example:
import "std/io";
let handle = io.file_open("data.txt", "r");
file_read
Read the entire contents of an open file handle as a string.
let content = io.file_read(handle);
File handle from file_open()
Complete file contents as a string
Example:
import "std/io";
let handle = io.file_open("data.txt", "r");
let content = io.file_read(handle);
io.file_close(handle);
println(content);
file_read_line
Read a single line from an open file handle. Returns void when end-of-file is reached.
let line = io.file_read_line(handle);
File handle from file_open()
The next line from the file (without trailing newline), or void at EOF
Example:
import "std/io";
let reader = io.file_open("data.txt", "r");
let line = io.file_read_line(reader);
while line != void {
println(line);
line = io.file_read_line(reader);
}
io.file_close(reader);
file_write
Write a string to an open file handle.
let bytes = io.file_write(handle, content);
File handle from file_open()
String content to write to the file
Example:
import "std/io";
let handle = io.file_open("data.txt", "w");
io.file_write(handle, "Line 1\n");
io.file_write(handle, "Line 2\n");
io.file_close(handle);
file_close
Close an open file handle. Always close handles when finished to free system resources.
Example:
import "std/io";
let handle = io.file_open("data.txt", "r");
let content = io.file_read(handle);
io.file_close(handle); // Always close when done
file_exists
Check if a file exists at the given path.
let exists = io.file_exists(path);
Path to check for existence
true if the path exists, false otherwise
Example:
import "std/io";
if io.file_exists("config.txt") {
let content = io.read_file("config.txt");
println(content);
} else {
println("Config file not found");
}
read_file
Convenience function to read an entire file in one operation. Opens, reads, and closes the file automatically.
let content = io.read_file(path);
Complete file contents as a string
Example:
import "std/io";
// Simple one-line file reading
let content = io.read_file("hello.txt");
println(content);
write_file
Convenience function to write an entire file in one operation. Opens, writes, and closes the file automatically.
io.write_file(path, content);
Path to the file to write
String content to write to the file
Example:
import "std/io";
// Simple one-line file writing
io.write_file("hello.txt", "Hello, Walrus!");
Usage patterns
Simple read/write
For small files or one-time operations, use the convenience functions:
import "std/io";
// Write
io.write_file("hello.txt", "Hello, Walrus!");
// Read
let content = io.read_file("hello.txt");
println(content); // Hello, Walrus!
Handle-based operations
For larger files or streaming, use handles:
import "std/io";
// Writing multiple lines
let handle = io.file_open("data.txt", "w");
io.file_write(handle, "Line 1\n");
io.file_write(handle, "Line 2\n");
io.file_close(handle);
Line-by-line reading
Process large files one line at a time:
import "std/io";
let reader = io.file_open("data.txt", "r");
let line = io.file_read_line(reader);
while line != void {
println(line);
line = io.file_read_line(reader);
}
io.file_close(reader);
Conditional file operations
Check before reading or writing:
import "std/io";
if io.file_exists("config.txt") {
let config = io.read_file("config.txt");
println(f"Loaded config: {config}");
} else {
println("No config file found, using defaults");
io.write_file("config.txt", "default settings");
}