reading file byLine

Namal via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Wed Sep 2 14:53:18 PDT 2015


Thx guys, this helped alot. The next thing I want to do is read 
the file line by line and split the stream into words. I found 
this example of code that seems to do sort of something like it. 
How can I modyfy it so I can store the words in an array of 
strings? Is a => a.length the iterator range?


import std.algorithm, std.stdio, std.string;
// Count words in a file using ranges.
void main()
{
     auto file = File("file.txt"); // Open for reading
     const wordCount = file.byLine()            // Read lines
                           .map!split           // Split into words
                           .map!(a => a.length) // Count words per 
line
                           .sum();              // Total word count
     writeln(wordCount);
}


More information about the Digitalmars-d-learn mailing list