The File: module provides file I/O. File handles are integer IDs assigned automatically by the engine when you call File:Open.
| Call | Effect |
|---|---|
File:Open(path, mode, handle) |
Open file; mode is "r", "w", or "a"; handle → outVar |
File:Close(handle) |
Close one file |
File:CloseAll() |
Close all open files |
File:Write(text, handle) |
Write string to file |
File:Read(handle, outVar) |
Read next whitespace-delimited token → outVar |
File:ReadLine(handle, outVar) |
Read next line → outVar |
File:ReadAll(handle, outVar) |
Read entire remaining file content → outVar |
File:EOF(handle, outVar) / File:EndOfFile(handle, outVar) |
true if at end of file → outVar |
File:Exists(path, outVar) |
true if file exists → outVar |
File:Delete(path, outVar) |
Delete file; true on success → outVar |
Var:Number h;
File:Open("data.txt", "w", h);
File:Write("hello\n", h);
File:Close(h);
Var:Boolean exists;
File:Exists("data.txt", exists);
Konsol:Print(exists); // true
// Read entire file at once
Var:String content;
Var:Number h2;
File:Open("data.txt", "r", h2);
File:ReadAll(h2, content);
File:Close(h2);
Konsol:Print(content); // hello
// Read line by line
Var:Number h3;
Var:Boolean eof;
Var:String line;
File:Open("data.txt", "r", h3);
File:EOF(h3, eof);
while (eof == false) {
File:ReadLine(h3, line);
Konsol:Print(line);
File:EOF(h3, eof);
}
File:Close(h3);
// 12_file.ks — File module (write, read, readLine, EOF, exists, delete)
Konsol:Print("=== File Module ===");
Var:String path = "test_output.txt";
Var:Boolean exists;
Var:Boolean eof;
Var:String line;
Var:String token;
Var:Boolean deleted;
Var:Number fh;
Var:Number fh2;
// ── Write ─────────────────────────────────────────────────────────────────────
Konsol:Print("--- writing ${path} ---");
File:Open(path, "w", fh);
File:Write("line one\n", fh);
File:Write("line two\n", fh);
File:Write("line three\n", fh);
File:Write("42\n", fh);
File:Close(fh);
Konsol:Print("written and closed");
// ── Exists ────────────────────────────────────────────────────────────────────
File:Exists(path, exists);
Konsol:Print("Exists: ${exists}");
File:Exists("no_such_file.txt", exists);
Konsol:Print("Exists (missing): ${exists}");
// ── ReadLine until EOF ────────────────────────────────────────────────────────
Konsol:Print("--- reading lines ---");
File:Open(path, "r", fh);
Var:Number lineNum = 1;
File:EOF(fh, eof);
while (eof == false) {
File:ReadLine(fh, line);
Konsol:Print("line ${lineNum} : ${line}");
lineNum++;
File:EOF(fh, eof);
}
File:Close(fh);
// ── Append ────────────────────────────────────────────────────────────────────
Konsol:Print("--- appending ---");
File:Open(path, "a", fh);
File:Write("appended line\n", fh);
File:Close(fh);
// Re-read with Read (whitespace-delimited tokens)
Konsol:Print("--- re-read tokens ---");
File:Open(path, "r", fh);
Var:Number i = 0;
File:EOF(fh, eof);
while (eof == false && i < 6) {
File:Read(fh, token);
Konsol:Print("token: ${token}");
i++;
File:EOF(fh, eof);
}
File:Close(fh);
// ── CloseAll ──────────────────────────────────────────────────────────────────
File:Open(path, "r", fh);
File:Open(path, "r", fh2);
Konsol:Print("closing all handles");
File:CloseAll();
// ── ReadAll ───────────────────────────────────────────────────────────────────
Konsol:Print("--- ReadAll ---");
File:Open(path, "r", fh);
Var:String allContent;
File:ReadAll(fh, allContent);
File:Close(fh);
Konsol:Print(allContent);
// ── Delete ────────────────────────────────────────────────────────────────────
Konsol:Print("--- deleting ${path} ---");
File:Delete(path, deleted);
Konsol:Print("Delete returned: ${deleted}");
File:Exists(path, exists);
Konsol:Print("Exists after delete: ${exists}");