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.

Function std.stdio.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.

auto byLineCopy(Terminator, Char) (
  KeepTerminator keepTerminator = No.keepTerminator,
  Terminator terminator = '\n'
)
if (isScalarType!Terminator);

auto byLineCopy(Terminator, Char) (
  KeepTerminator keepTerminator,
  Terminator terminator
)
if (is(immutable(ElementEncodingType!Terminator) == immutable(Char)));

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.

Parameters

NameDescription
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 Line separator ('\n' by default). Use newline for portability (unless the file was opened in text mode).

Example

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

readText

Authors

Walter Bright, Andrei Alexandrescu, Alex Rønne Petersen

License

Boost License 1.0.