Skip to content

Core\IO::stdin

Reads all of the text that was sent to a program's standard input.

Standard input is the text a program receives when it is started. In a terminal you send a file to it with <, as in nvs run report.nvs < orders.csv, or with a pipe from another program.

Core\IO::stdin reads until the input ends and returns everything as one string. A program started with no input gets the empty string. The input can be read only once, so a second call returns the empty string too. In a terminal with no file or pipe, the call waits until the person there ends the input with Ctrl-D (Ctrl-Z and Enter on Windows).

The result is tainted, because the text comes from outside the program. When the input is not valid UTF-8, stdin throws a RuntimeError. It needs no capability.

The examples below count the lines of the input, show what a program with no input reads, and add up a stock list.

Return value

Returns tainted string. Every byte until the end of input, as one tainted string. It is the empty string when the input is already closed. A program started with no input gets that. Input ends when the writer closes it, so at a terminal this call waits for the user. To ask the user a question, use the prompts in Core\Cli, which have a time limit.

Errors

  • IOErrorThe read failed in the operating system. The process writing to the pipe stopped, or standard input was not open for reading.
  • RuntimeErrorThe input is not valid UTF-8. The message gives the position of the first byte that is not text.

Examples

Count the lines of the input

<?nvs
// Shows how to read the text that was sent to a program's standard input.
// Start it with `nvs run 01-count-the-lines-of-the-input.nvs < notes.txt`.
// Here the input is the file `01-count-the-lines-of-the-input.in` beside this one.
// `Core\IO::stdin` returns all of the input as one string.
tainted string $input = Core\IO::stdin();
// `Core\Str::lines` splits the text into lines. This prints 3 and 53.
array<tainted string> $lines = Core\Str::lines($input);
echo 'lines: ', Core\Arr::count($lines), "\n";
echo 'characters: ', Core\Str::length($input), "\n";
// The last line of the input is printed as it is.
echo 'last line: ', $lines[Core\Arr::count($lines) - 1], "\n";
Output
lines: 3
characters: 53
last line: Send the report to Café Größe

No input gives an empty string

<?nvs
// Shows what a program reads when nothing was sent to its standard input.
// This program is started with no input, so `Core\IO::stdin` returns "".
tainted string $input = Core\IO::stdin();
if ($input == '') {
echo "No input was given.\n";
echo "usage: nvs run 02-no-input-gives-an-empty-string.nvs < file.txt\n";
} else {
echo 'read ', Core\Str::length($input), " characters\n";
}
// A second call returns "" too. The input can be read only once.
echo 'second call: ', Core\Str::length(Core\IO::stdin()), " characters\n";
Output
No input was given.
usage: nvs run 02-no-input-gives-an-empty-string.nvs < file.txt
second call: 0 characters

Add up a stock list

<?nvs
// Shows a small command-line tool that adds up a stock list sent to its input.
// Start it with `nvs run 03-add-up-a-stock-list.nvs < stock.csv`. Here the input
// is the file `03-add-up-a-stock-list.in` beside this one.
class Stock
{
public static function total(tainted string $input): void
{
int $total = 0;
uint $lineNumber = 0;
foreach (Core\Str::lines($input) as tainted string $line) {
$lineNumber = $lineNumber + 1;
// Each line is a product and a count, separated by a `;`.
array<tainted string> $cells = Core\Str::split($line, ';');
if (Core\Arr::count($cells) != 2) {
echo 'line ', $lineNumber, " is skipped: it needs two fields\n";
continue;
}
// `as ?int` converts the text to a whole number. If that is not possible,
// the result is `null`.
?int $count = Core\Str::trim($cells[1]) as ?int;
if ($count == null) {
echo 'line ', $lineNumber, " is skipped: '", $cells[1], "' is not a number\n";
continue;
}
$total = $total + $count;
}
// This prints 57.
echo 'items in stock: ', $total, "\n";
}
}
Stock::total(Core\IO::stdin());
Output
line 3 is skipped: 'several' is not a number
line 5 is skipped: it needs two fields
items in stock: 57