File.byLineCopy

Returns an input range set up to read from the file handle one line at a time. Each line will be newly allocated. front will cache its value to allow repeated calls without unnecessary allocations.

Note: Due to caching byLineCopy can be more memory-efficient than File.byLine.map!idup.

The element type for the range will be Char[]. Range primitives may throw StdioException on I/O error.

  1. auto byLineCopy(KeepTerminator keepTerminator, Terminator terminator)
    struct File
    byLineCopy
    (
    Terminator = char
    Char = immutable char
    )
    (
    KeepTerminator keepTerminator = No.keepTerminator
    ,
    Terminator terminator = '\n'
    )
    if (
    isScalarType!Terminator
    )
  2. auto byLineCopy(KeepTerminator keepTerminator, Terminator terminator)

Parameters

Char

Character type for each line, defaulting to immutable char.

keepTerminator KeepTerminator

Use Yes.keepTerminator to include the terminator at the end of each line.

terminator Terminator

Line separator ('\n' by default). Use std.ascii.newline for portability (unless the file was opened in text mode).

Examples

import std.algorithm, std.array, std.stdio;
// Print sorted lines of a file.
void main()
{
    auto sortedLines = File("file.txt")   // Open for reading
                       .byLineCopy()      // Read persistent lines
                       .array()           // into an array
                       .sort();           // then sort them
    foreach (line; sortedLines)
        writeln(line);
}

See Also

Meta