View source code
Display the source code in std/stdio.d from which this page was generated on
github.
Improve this page
Quickly fork, edit online, and submit a pull request for this page.
Requires a signed-in GitHub account. This works well for small changes.
If you'd like to make larger changes you may want to consider using
local clone.
Page wiki
View or edit the community-maintained wiki page associated with this page.
Struct std.stdio.lines
Iterates through the lines
of a file by using foreach
.
Constructors
Name | Description |
---|---|
this
|
Constructor. |
Example
void main() { foreach (string line; lines(stdin)) { ... use line ... } }
The line terminator ('\n'
by default) is part of the string read (it
could be missing in the last line of the file). Several types are
supported for line
, and the behavior of
changes accordingly:
lines
- If
line
has typestring
,wstring
, ordstring
, a new string of the respective type is allocated every read. - If
line
has typechar[]
,wchar[]
,dchar[]
, the line's content will be reused (overwritten) across reads. - If
line
has typeimmutable(ubyte)[]
, the behavior is similar to case (1), except that no UTF checking is attempted upon input. - If
line
has typeubyte[]
, the behavior is similar to case (2), except that no UTF checking is attempted upon input.
In all cases, a two-symbols versions is also accepted, in which case
the first symbol (of integral type, e.g. ulong
or uint
) tracks the zero-based number of the current line.
Example
foreach (ulong i, string line; lines(stdin)) { ... use line ... }
In case of an I/O error, an
is thrown.
StdioException
See Also
byLine
Authors
Walter Bright, Andrei Alexandrescu, Alex Rønne Petersen