Files & IO
These modules cover the first practical “real program” tasks:
- read and write files
- print text
- read a line from the terminal
std.fs
Use std.fs for local file and directory work.
cneg
import std.fs as fs;Current API:
fs.exists(str) -> boolfs.cwd() -> result strfs.create_dir(str) -> result boolfs.remove_dir(str) -> result boolfs.read_text(str) -> result strfs.file_size(str) -> result intfs.copy(str, str) -> result boolfs.write_text(str, str) -> result boolfs.append_text(str, str) -> result boolfs.rename(str, str) -> result boolfs.move(str, str) -> result boolfs.remove(str) -> result bool
Small example
cneg
import std.strings as strings;
import std.fs as fs;
fn:int main() {
let text:str = strings.copy("demo");
let write_ok:result bool = fs.write_text("build/demo.txt", text);
if write_ok.ok == false {
free text;
return 1;
}
let size:result int = fs.file_size("build/demo.txt");
if size.ok == false {
free text;
return 2;
}
free text;
return 0;
}What to notice:
- file operations can fail, so many return
result ... strings.copy(...)created owned text, so it is freed
std.io
Use std.io for terminal-style input/output.
cneg
import std.io as io;Current API:
io.write(str) -> voidio.write_line(str) -> voidio.read_line() -> str
Small example
cneg
import std.io as io;
import std.strings as strings;
fn:int main() {
let name:str = io.read_line();
let message:str = strings.concat("hello ", name);
io.write_line(message);
free name;
free message;
return 0;
}ownership
io.read_line() returns an owned string. Free it after use.
Beginner recommendation
If you are new, start with:
io.write_line(...)io.read_line()fs.write_text(...)fs.read_text(...)
That gives you enough to build small text tools very quickly.