WriteString¶
Status: Stable
documented, exercised by the test suite and/or worked examples, with no known limitations recorded.
Description¶
WriteString[stream, str1, str2, ...]
writes the strings to an output stream with no added quotes or newline. Non-string arguments are written in input form.
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)¶
Raw: no quotes, no separators, no newline
The whole line is "abc"
Embed newlines yourself
Implementation notes¶
Algorithm. builtin_writestring resolves its first argument with resolve_output exactly
as Write does (an OutputStream, or a name auto-opened truncating and left open;
$Failed otherwise). It then writes each remaining argument verbatim: a string is fputs'd
raw, with no surrounding quotes, and a non-string argument is rendered with expr_to_string
first. It adds no separators between arguments and no trailing newline, then fflushes and
returns Null.
Data structures ā the stream layer. Writes to the FILE* of an output slot in the
process-global Stream registry described under OpenRead. It is the exact-byte
counterpart to Write: Write prints input form and appends '\n', whereas
WriteString emits precisely the bytes given ā so a caller lays out CSV lines, headers, or any
newline structure explicitly (e.g. WriteString[s, "1 2 3\n4 5 6\n"]).
Complexity / limits. O(total output length); fflush per call. Pure ANSI C99
(fputs/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: Write, OutputStream, Read, ReadList
- Source:
src/io/streams.c - Specification:
docs/spec/builtins/file-io.md - Tests:
tests/test_streams.c
Notes & additional examples¶
Notes¶
WriteString[stream, s1, s2, ...] writes the strings verbatim ā no surrounding
quotes, no separators between arguments, and no trailing newline. It is the tool
for laying out an exact byte stream (a CSV line, a header, a line you terminate
with an explicit "\n"), in contrast to Write, which prints input
form and appends a newline.
A non-string argument is written in input form. Output is flushed after each call.
stream may be an OutputStream, a "file", or File["file"]; a named file that
is not already open is auto-opened (truncating) and left open. WriteString
returns Null.