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 = No.keepTerminator, Terminator terminator = '\n')
  2. auto byLineCopy(KeepTerminator keepTerminator, Terminator terminator)
    byLineCopy
    (
    Terminator
    Char = immutable char
    )
    if (
    is(Unqual!(ElementEncodingType!Terminator) == Unqual!Char)
    )

Parameters

Char

Character type for each line, defaulting to immutable char.

keepTerminator

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

terminator
Type: Terminator

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

Examples

1 import std.algorithm, std.array, std.stdio;
2 // Print sorted lines of a file.
3 void main()
4 {
5     auto sortedLines = File("file.txt")   // Open for reading
6                        .byLineCopy()      // Read persistent lines
7                        .array()           // into an array
8                        .sort();           // then sort them
9     foreach (line; sortedLines)
10         writeln(line);
11 }

See Also

Meta

Suggestion Box / Bug Report