line terminators

rassoc rassoc at
Wed Sep 28 21:17:16 UTC 2022

On 9/28/22 21:36, NonNull via Digitalmars-d-learn wrote:
> If I want to read a text file line by line, treating any one of these things as a newline, there would seem to be no canned way to do that in std.stdio .
> 1.) What is the best way to achieve this result in D?
If you have structured data, you can use byRecord [1] to read the important parts right into a tuple.

Should it be unstructured data, then there's lineSplitter [2] which handles all of the above newline specifics, I think. Sadly, it's working on text and not on files.

So, for small files you could just read the whole file via readText [3] and process it via lineSplitter.

If the file is rather large, then there's the option to use memory-mapped files instead:

import std;
void main() {
	scope mmfile = new MmFile("largefile.txt");
	auto data = cast(string) mmfile[];
	foreach (line; data.lineSplitter) {
		// process line, os handles memory-mapped file buffering

Hope that helps.


More information about the Digitalmars-d-learn mailing list