View source code Display the source code in std/stdio.d from which this page was generated on github. 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. Page wiki View or edit the community-maintained wiki page associated with this page.

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.

Prototypes

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

auto byLineCopy(Terminator, Char)(
  KeepTerminator keepTerminator,
  Terminator terminator
)
if (is(Unqual!(ElementEncodingType!Terminator) == Unqual!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 KeepTerminator.yes to include the terminator at the end of each line.
terminator Line separator ('\n' by default). Use std.ascii.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

std.file.readText

Authors

Walter Bright, Andrei Alexandrescu, Alex Rønne Petersen

License

Boost License 1.0.

Comments