Write¶
Status: Stable
documented, exercised by the test suite and/or worked examples, with no known limitations recorded.
Description¶
Write[stream, expr1, expr2, ...]
writes the expressions to an output stream in input form, followed by a newline. The stream may be an OutputStream, a "file", or File["file"]; a named file that is not already open is opened for writing and left open.
Examples (13)¶
Every input below was run against the current Mathilda build and its output recorded.
Basic Examples (8)¶
Write evaluates, prints input form, adds a newline
WriteString writes raw text, no added newline
1 + 1 was evaluated to 2 before writing
Hold writes the unevaluated form
Applications (5)¶
Each Write evaluates, then adds a newline
1 + 1 was evaluated to 2 before writing
Hold writes the unevaluated form
Implementation notes¶
Algorithm. builtin_write resolves its first argument with resolve_output: an
OutputStream object, or a "file"/File["file"] name that is auto-opened (truncating)
and left open; an InputStream or an unopenable name fails with $Failed (Write::openx /
Write::noopen). For each remaining argument it renders the expression with expr_to_string
(re-readable input form) and fputs it, then writes a single '\n' and fflushes. Returns
Null. Because its arguments are ordinary (evaluated) arguments, 1 + 1 is written as 2;
Hold[...] is the way to write an unevaluated form.
Data structures ā the stream layer. Writes to the FILE* of an output slot in the
process-global Stream registry described under OpenRead. The fflush after
each call is what makes the bytes immediately visible to Read/ReadList,
so a write-then-read round trip works without an intervening Close.
Complexity / limits. O(total output length). The trailing newline per call is what makes
the output parse back one object per Read. Differs from WriteString,
which writes strings raw with no quoting and no added newline. Pure ANSI C99
(fputs/fputc/fflush). ATTR_PROTECTED.
Protected. Return$Failedif the file cannot be opened.Writeevaluates its expression arguments before writing (useHold[...]to write an unevaluated form), and output is flushed after each call so it round-trips withRead/ReadList.
Attributes: Protected.
References¶
See also: WriteString, OutputStream, Read, ReadList
- Source:
src/io/streams.c - Specification:
docs/spec/builtins/file-io.md - Tests:
tests/test_streams.c
Notes & additional examples¶
Notes¶
Write[stream, e1, e2, ...] writes each expression in re-readable input form,
followed by a single newline, and flushes ā so the output round-trips through
Read/ReadList. Expression arguments are evaluated
first (1 + 1 becomes 2); wrap in Hold[...] to write the unevaluated form.
stream may be an OutputStream, a "file" string, or File["file"]; a named
file that is not already open is auto-opened (truncating) and left open. Writing
raw text with no quoting or added newline is WriteString's job.
Write returns Null.