File I/O

Astra's file system runs every operation through a sandbox — a temporary copy of your file is made, all reads and writes happen there, and the result is flushed back to disk only when you call close(). No partial writes, no corruption on crash.

📂
Open & Close
create() opens or creates a file. close() saves and flushes.
âœī¸
Write Modes
Append last, append first, or overwrite raw — your choice.
🔍
Precise Reads
Read line-by-line, by line number, by character count, or all at once.

create() & close()

Every file operation starts with create(), which returns a file handle. Pass that handle to every subsequent operation. Always call close()when done — that's what actually writes changes back to disk.

file-basic.astra
f = create("D:/notes.txt") \\ ... do stuff with f ... close(f)
âš ī¸ If you skip close(f), your writes stay in the sandbox and are lost when the program ends.

If the file doesn't exist yet, create() makes an empty one. If it already exists, it copies it into the sandbox for safe editing.

plus() — Writing to a File

Use plus(handle, text, mode) to write. There are three modes:

ModeMeaning
"la"Line Append — adds text as a new line at the end
"fa"First Append — inserts text as a new line at the top
"raw"Overwrites the entire file with exactly the given text

Append to end — "la"

write-la.astra
f = create("D:/log.txt") plus(f, "Hello from Astra!", "la") plus(f, "Line 2", "la") plus(f, "Line 3", "la") close(f) \\ log.txt: \\ Hello from Astra! \\ Line 2 \\ Line 3

Prepend to top — "fa"

f = create("D:/log.txt") plus(f, "=== Latest entry ===", "fa") close(f) \\ log.txt: \\ === Latest entry === \\ Hello from Astra! \\ Line 2 \\ Line 3

Overwrite entire file — "raw"

f = create("D:/data.json") plus(f, "{"name":"Ravi","age":25}", "raw") close(f)
💡 "raw" is useful when writing JSON or config files where you want full control over every byte.

read() — Reading from a File

Use read(handle, mode) to read content. Two modes are available:

ModeMeaning
"l"Read the next line (advances an internal cursor)
"t"Read the entire file as one string

Read line by line

read-lines.astra
f = create("D:/log.txt") repeat (eof(f) == false) line = read(f, "l") write line ; close(f)

Read entire file at once

f = create("D:/data.json") content = read(f, "t") close(f) write content

fetch() — Precise Fetching

fetch(handle, mode, pos1, pos2) lets you pull specific parts of a file without reading everything into memory.

Modepos1pos2Returns
"l"line number—The full line at that number
"c"char count—First N characters of the file
"lc"line numberchar countFirst N chars of that line
"count"——Total number of lines (as integer)

Get a specific line

fetch.astra
f = create("D:/log.txt") line2 = fetch(f, "l", 2) write line2 \\ Line 2 close(f)

Count total lines

f = create("D:/log.txt") total = fetch(f, "count") write total \\ 3 close(f)

Get first N characters of a line

f = create("D:/log.txt") preview = fetch(f, "lc", 1, 5) write preview \\ Hello close(f)

eof() — End of File Check

eof(handle) returns true when there are no more lines to read. Use it as the condition in a repeat loop.

eof.astra
f = create("D:/names.txt") repeat (eof(f) == false) name = read(f, "l") write "Name: " + name ; close(f)
â„šī¸ eof() tracks the internal read cursor. It resets to the start each time you call create() on the same path.

clear() & clearLine()

clear() — Erase the whole file

Wipes all content from the file. The handle stays open — you can keep writing after.

f = create("D:/log.txt") clear(f) plus(f, "Fresh start", "la") close(f)

clearLine() — Remove lines containing text

Removes every line that contains the given string.

f = create("D:/log.txt") clearLine(f, "Line 2") close(f) \\ "Line 2" is gone, rest remains
âš ī¸ clearLine() removes all lines that contain the substring — not just the first match.

Sandbox Safety

When you call create(), Astra copies your file into a temporary sandbox directory. All operations work on that copy. Your original file is only updated when you call close().

💡 Think of create() / close() like a transaction — nothing is committed until you close.

Common Patterns

Write and read back

write-read.astra
\\ Write f = create("D:/test.txt") plus(f, "Hello from Astra!", "la") plus(f, "Line 2", "la") plus(f, "Line 3", "la") close(f) \\ Read back f2 = create("D:/test.txt") repeat (eof(f2) == false) line = read(f2, "l") write line ; close(f2)

Write JSON, read it back

json-file.astra
\\ Write JSON f = create("D:/data.json") plus(f, "{"name":"Ravi","age":25}", "raw") close(f) \\ Read and parse f2 = create("D:/data.json") content = read(f2, "t") close(f2) parseJson("data", content) write data1:name \\ Ravi write data1:age \\ 25

Log with line count

f = create("D:/log.txt") plus(f, "Entry A", "la") plus(f, "Entry B", "la") plus(f, "Entry C", "la") total = fetch(f, "count") write "Total lines: " + total \\ Total lines: 3 close(f)