View source code
Display the source code in std/stdio.d from which this
page was generated on github.
Report a bug
If you spot a problem with this page, click here to create a
Bugzilla issue.
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.
Struct std.stdio.lines
Iterates through the lines of a file by using foreach
.
struct lines
;
Constructors
Name | Description |
---|---|
this
(f, terminator)
|
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 lines
changes accordingly:
- 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 StdioException
is thrown.
See Also
byLine
Authors
Walter Bright, Andrei Alexandrescu, Alex Rønne Petersen
License
Copyright © 1999-2022 by the D Language Foundation | Page generated by ddox.