Browser virtual files
ASPL 0.7.0 · Current release
Handle not-found, write exact text bytes and distinguish temporary files from saved source.
On this page
What you will learn
Handle a missing file, write hello followed by a newline to lesson.txt, and read it back. Explain when temporary browser run files survive and how to preserve their exact bytes.
Before you begin
Read strings and recoverable results. Open this independent program as a new Home draft and choose Run explicitly. Start with a clean Run files collection in that intended draft so missing.txt is absent. Opening the example prepares source only and neither imports files nor runs it. Earlier drafts remain available.
Read, handle failure, write and read again
read_text_file("missing.txt") / اقرأ_ملف_نصي(...) returns result<string> / نتيجة<نص>. Test ::ok / ::نجاح first. When absent, only the failure branch reads ::error::code / ::الخطأ::الرمز and prints not-found with \n. If the file exists, only the success branch reads ::value / ::قيمة. The code remains guarded in either case.
Execution: Browser and native · ASPL 0.7.0
- Start with a clean run-file collection. This program creates or replaces lesson.txt. Browser data is temporary; native paths are relative to the process working directory.
Input: No input lines are required.
fn start_main() -> i64 {
let missing: result<string> = read_text_file("missing.txt");
if (missing::ok) {
print(missing::value);
} else {
print(missing::error::code + "\n");
}
let saved: result<unit> = write_text_file("lesson.txt", "hello\n");
if (saved::ok) {
let loaded: result<string> = read_text_file("lesson.txt");
if (loaded::ok) {
print(loaded::value);
} else {
print(loaded::error::code + "\n");
}
} else {
print(saved::error::code + "\n");
}
return 0;
}
Expected outcomes
{
"browser": {
"check": "success",
"class": "success",
"output": "not-found\nhello\n",
"processStatus": 0,
"fileUpdates": [
{
"name": "lesson.txt",
"content": "hello\n"
}
]
},
"native": {
"check": 0,
"status": 0,
"output": "not-found\nhello\n"
}
}write_text_file("lesson.txt", "hello\n") / اكتب_ملف_نصي(...) creates or replaces the file with UTF-8 text. It returns result<unit> / نتيجة<وحدة>: a successful write has no readable value, so test success rather than reading saved::value. Only a failed write supplies error information. Inside successful write handling, another read returns text; its success branch prints that text directly. It already includes the newline, so print adds none. Read and write failures remain recoverable results. A trap still ends execution. The entry returns normal status 0 after its branches, independently of printed data or file contents.
Predict the output and bytes
With a clean collection, exact output is not-found\nhello\n, with status 0. lesson.txt contains exactly six bytes: hexadecimal 68 65 6c 6c 6f 0a. There is one LF newline, no extra newline and no byte-order mark. Inspect Run files after the run and explicitly download lesson.txt to preserve those bytes outside the page.
Files can survive another run in the same draft's current document. A second run still handles absent missing.txt and replaces lesson.txt with the same bytes. Refresh clears all run files, output, input transcripts and worker state, while saved source remains. Saving source does not save run files. Download before refresh if you need the data. Switching reading language does not translate source or bytes.
These are temporary execution data with flat names in browser memory, not native filesystem paths or source projects. Importing a source-looking run filename such as helper.aspl does not enable module loading. User source modules require native execution. The same text-file functions run natively against paths relative to the process working directory; use an empty temporary directory for this example there. See the file guide.
Change the saved text
Change only the written string to "hello again\n". Begin without missing.txt. Expected output is exactly not-found\nhello again\n, with status 0; lesson.txt now contains the twelve UTF-8 bytes of hello again followed by LF.
Write hello again followed by one newline, then predict the readback and download.
Hint: Edit only the second argument to write_text_file. Keep the filename, every result guard and the direct print of loaded text.
Show worked answer
The absent missing.txt still returns not-found and execution continues. The write replaces lesson.txt with hello again and one LF. Its successful branch reads the same file and prints those exact bytes, adding no second newline. The download preserves the twelve bytes; a refresh removes the browser copy but restores saved source. The entry returns normal status 0.
Execution: Browser and native · ASPL 0.7.0
- Start with a clean run-file collection. This program creates or replaces lesson.txt. Browser data is temporary; native paths are relative to the process working directory.
Input: No input lines are required.
fn start_main() -> i64 {
let missing: result<string> = read_text_file("missing.txt");
if (missing::ok) {
print(missing::value);
} else {
print(missing::error::code + "\n");
}
let saved: result<unit> = write_text_file("lesson.txt", "hello again\n");
if (saved::ok) {
let loaded: result<string> = read_text_file("lesson.txt");
if (loaded::ok) {
print(loaded::value);
} else {
print(loaded::error::code + "\n");
}
} else {
print(saved::error::code + "\n");
}
return 0;
}
Expected outcomes
{
"browser": {
"check": "success",
"class": "success",
"output": "not-found\nhello again\n",
"processStatus": 0,
"fileUpdates": [
{
"name": "lesson.txt",
"content": "hello again\n"
}
]
},
"native": {
"check": 0,
"status": 0,
"output": "not-found\nhello again\n"
}
}Check your understanding
Explain why a failed read can continue, why a successful write has no value payload, and what refresh restores. Predict the first branch if you import missing.txt with text before running. No grade or progress is stored, and navigation stays open. You have reached lesson 12 of the browser course. Return to the Tutorial path or read the text-file API.