lines

Iterates through the lines of a file by using foreach.

Constructors

this
this(File f, dchar terminator)

Constructor.

Examples

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:

  1. If line has type string, wstring, or dstring, a new string of the respective type is allocated every read.
  2. If line has type char[], wchar[], dchar[], the line's content will be reused (overwritten) across reads.
  3. If line has type immutable(ubyte)[], the behavior is similar to case (1), except that no UTF checking is attempted upon input.
  4. If line has type ubyte[], 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.

foreach (ulong i, string line; lines(stdin))
{
  ... use line ...
}

In case of an I/O error, an StdioException is thrown.

See Also

Meta