Whole-file byte and UTF-8 text operations built on the platform C stdio library.
File model
loadFile,saveFile,loadString,saveString, andappendStringoperate on whole-file transfers. The raw C bindings below also support stream-based access; the module defines no managed file-handle type or directory traversal.- Raw byte helpers use
Nat8Array or an object exposingdataandsize. - File-name inputs accept known Text, StringView, and String. Convert an unknown Text filename with
makeStringViewortoStringfirst. The text argument ofsaveStringandappendStringaccepts Text (known or unknown), StringView, or String. loadFileandloadStringrequire a seekable file whose reported end position is its byte length, within the supported size range; pseudo-files can report a misleading length and load as empty with success.loadStringdoes not validate UTF-8; it stores the bytes in aString.
Platform errno
For loadFile and saveFile, add the target platform's standard-library directory to the compiler include path and preload its errno module at global level. On Linux, use -I path/to/mpl-sl -I path/to/mpl-sl/linux with "errno" use (or "linux/errno" use). Use the corresponding macos or windows directory/module for those targets. An include path alone does not satisfy the global-preload requirement. Without the preload the compiler reports File «errno.mpl» was not initially loaded at the global level at the first loadFile or saveFile call.
Imported C functions
Stream/buffer/string addresses and fread/fwrite sizes, counts, and results are Natx. Other declared results, offsets, origins, and error numbers are Int32. Stream and buffer pointers are borrowed caller-owned storage; returned stream pointers remain valid until the corresponding close operation.
- Returns zero on success and a nonzero result on failure. The stream is no longer usable after the call; callers must not retry with the same stream.
- Returns zero when no error indicator is set and nonzero otherwise. This is not an errno value.
- Returns zero on success and a nonzero result on failure.
fopen
(mode filename -- stream) Opens the zero-terminated mode and filename strings and returns a stream pointer.- The filename and mode are zero-terminated strings. Returns address zero on failure; otherwise the caller must eventually close the returned stream.
fread
(stream count size buffer -- items) Reads count items of size bytes into buffer and returns the item count.- Returns complete items transferred, not bytes. A smaller count can indicate EOF or an error for reading; inspect the error indicator. The buffer must cover the requested byte range.
fseek
(origin offset stream -- result) Moves the stream position by the Int32 offset from the Int32 origin.- Returns zero on success; a nonzero result reports failure, subject to the current binding's width limitation.
Known issue: the current bindings declare fseek's offset and ftell's result as Int32. On 64-bit Linux, negative offsets are not passed correctly to the native long parameter, and large positions are truncated; restrict these bindings to verified nonnegative offsets and positions in the Int32 range; they are not a portable large-file interface.
- Returns a position or
-1on error, subject to the current binding's width limitation.
See the fseek known-issue note for the shared width limitation.
fwrite
(stream count size buffer -- items) Writes count items of size bytes from buffer and returns the item count.- Returns complete items written, not bytes. With a nonzero item size, writing fewer than requested indicates a write failure; inspect
ferror. A zero-size transfer writes nothing. The source buffer must cover the requested byte range.
Whole-file helpers
getErrnoText
(errnum -- textView) Returns an immutable, non-owning StringView for the supplied platform error number.- The view is valid while the platform error-text storage remains valid; it does not own the text.
loadFile
(name -- record) Loads one whole file into a byte-array record.record.resultis an error String, empty on success;record.datais theNat8Array of bytes read, owned by the record.- Open failures from
loadFileandsaveFileusefopen failed,plus the platform errno text. - On failure, discard
record.data. Its size can reflect the planned allocation rather than the bytes read; contents beyond the successful read are not valid file data.
saveFile
(data name -- result) Saves a contiguous byte source to one file and returns an error String.saveFileexpects a contiguous source whosesizeis a byte count and whosedataexposes a reference to at least that many bytes. A numeric address alone is not the data reference expected here. ANat8Array, aString, or aStringViewfits this contract; an Array of wider elements needs an explicit byte view/count.- File-name inputs are converted to zero-terminated strings. An embedded zero terminates the path; the suffix is not part of the file name. These binary-mode helpers write the source bytes without UTF-8 validation or newline conversion; a Text literal is not a valid data source, so convert it with
toStringormakeStringViewfirst. - An empty result means the open and
fwritechecks passed; it does not guarantee a complete write. See the Limits of the whole-file helpers.
loadString
(name -- record) Loads one whole file into a record containing success and a String.record.successis FALSE if open, seek, read, or close fails.record.datais the loaded, owned String. On failure, discardrecord.data; its reported size can include bytes not read from the file.- Bytes are stored without UTF-8 validation. See the Limits of the whole-file helpers.
- The result is TRUE only when opening, writing or empty-write handling, flushing, and closing succeed.
- The result is TRUE only when opening, writing or empty-write handling, flushing, and closing succeed.
Limits of the whole-file helpers
- See the File model for seekability and pseudo-file behavior.
- See
loadFilefor planned Array size and data disposal after a short read. loadFileandsaveFileignorefclose's result and leave the stream open after a failedfreadorfwrite; the String helpers always close the stream (saveStringandappendStringflush first).loadFileignoresfseekresults; neither loader checksftellfailure.fread failed,andfwrite failed,carrystrerror(ferror(stream)), not an errno text.- Use the raw bindings when these limits matter.
Known issue: A failed loadFile or saveFile transfer after opening leaves the stream open; use the raw bindings with explicit cleanup.
Examples
Compile-time example: signature-level result schemas
"control" use
"file" use
{} () {} [
"sample.txt" "alpha" saveString printStack _:;
"sample.txt" loadString.success printStack _:;
"sample.txt" loadString.data.size printStack _:;
"sample.txt" "beta" appendString printStack _:;
] "main" exportFunction
Expected Output During Compilation
Cond
Cond Cref
Int32
Cond
Runtime example: String helpers
"String" use
"control" use
"file" use
{} Int32 {} [
["sample.txt" "alpha" saveString] "save failed" ensure
["sample.txt" "beta" appendString] "append failed" ensure
loaded: "sample.txt" loadString;
[loaded.success] "load failed" ensure
loaded.data print
0
] "main" exportFunction
Expected Output
alphabeta
Runtime example: loadFile and saveFile
"Array" use
"String" use
"control" use
"file" use
"linux/errno" use
{} Int32 {} [
bytes: Nat8 Array;
104n8 @bytes.append 105n8 @bytes.append
saved: bytes "probe.bin" saveFile;
loaded: "probe.bin" loadFile;
missing: "no-such-file.bin" loadFile;
("save result=[" saved "]" LF) printList
("load result=[" loaded.result "] size=" loaded.data.size LF) printList
("missing result=[" missing.result "] size=" missing.data.size LF) printList
0
] "main" exportFunction
Expected Output
save result=[]
load result=[] size=2
missing result=[fopen failed, No such file or directory] size=0
See also
- String: UTF-8 string views, owned strings, formatting helpers, and text conversion utilities.
- Array: Growable array.
- Span: Contiguous non-owning view of items of the same schema.
- linux/errno · macos/errno · windows/errno: Accessor for platform error-number storage.