FilePrint¶
Status: Stable
documented, exercised by the test suite and/or worked examples, with no known limitations recorded.
Description¶
FilePrint["file"]
prints out the raw textual contents of file.
FilePrint["file", n]
prints out the first n raw textual lines of file.
FilePrint["file", -n]
prints out the last n raw textual lines of file.
FilePrint["file", m;;n]
prints out lines m through n of file.
FilePrint["file", m;;n;;s]
prints out lines m through n of file in steps of s; s may be negative to walk the range backwards.
Notes
FilePrint returns Null on success and $Failed if the file cannot be opened. Negative indices inside the Span count from the end of the file (-1 is the last line).Examples (1)¶
Every input below was run against the current Mathilda build and its output recorded.
Applications (1)¶
Implementation notes¶
Algorithm. builtin_fileprint slurps the whole file into one buffer (fileprint_slurp, binary mode via fopen/ftell/fread); on open failure it prints FilePrint::noopen and returns $Failed. fileprint_split_lines then records one LineSpan {start, len} per line in a single pass (each line's trailing \n is included in its length, and an unterminated final line is captured). The optional selector is decoded by fileprint_decode_selector into a 1-based (start, end, step) triple: a positive integer n selects the first n lines, negative the last |n|, and Span[m, n] / Span[m, n, s] an explicit (possibly reverse, signed-step) range with All and negative endpoints resolved by fileprint_resolve_span_slot. fileprint_emit writes the selected lines verbatim with fwrite (preserving embedded NULs / non-UTF-8 bytes), appending a \n only if the last emitted line lacked one. Returns the symbol Null; an ill-formed selector leaves the call unevaluated rather than printing partially. ATTR_PROTECTED.
Protected.- Bytes pass through verbatim via
fwrite, including embedded NULs and non-UTF-8 sequences. - Lines are 1-indexed; negative indices inside the
Spancount from the end (-1is the last line). Allmay appear in anySpanslot (All;;-1,1;;All;;2, ...) and resolves to that slot's natural endpoint.- A positive integer larger than the file's line count clamps to "print everything"; the same applies to
-n. - When the file's final line lacks a trailing
\nand the selection actually emits it,FilePrintadds one so the next REPL prompt isn't appended to the file content. - Bad selectors (zero step, wrong types, wrong arity) leave the call unevaluated rather than producing partial output.
- Returns
Nullon success and$Failed(with aFilePrint::noopendiagnostic) when the file cannot be opened.
Attributes: Protected.
References¶
See also: Span
- Source:
src/files.c - Specification:
docs/spec/builtins/file-io.md - Tests:
tests/test_files.c
Notes & additional examples¶
Notes¶
FilePrint["file"] writes the raw textual contents of the file to stdout and returns Null; it does not parse or evaluate the file (use Get for that).