StdioException on I/O error, or
UnicodeException on Unicode
conversion error.
Example:
// Read lines from `stdin` into a string
// Ignore lines starting with '#'
// Write the string to `stdout`
import std.stdio;
void main()
{
string output;
char[] buf;
while (stdin.readln(buf))
{
if (buf[0] == '#')
continue;
output ~= buf;
}
write(output);
}
This method can be more efficient than the one in the previous example because stdin.readln(buf) reuses (if possible) memory allocated for buf, whereas line = stdin.readln() makes a new memory allocation for every line.
For even better performance you can help readln by passing in a large buffer to avoid memory reallocations. This can be done by reusing the largest buffer returned by readln:
Example:
// Read lines from `stdin` and count words
import std.array, std.stdio;
void main()
{
char[] buf;
size_t words = 0;
while (!stdin.eof)
{
char[] line = buf;
stdin.readln(line);
if (line.length > buf.length)
buf = line;
words += line.split.length;
}
writeln(words);
}
This is actually what byLine does internally, so its usage is recommended if you want to process a complete file.