Read Complete File to Array of Lines
Graham Fawcett
fawcett at uwindsor.ca
Fri May 11 11:31:39 PDT 2012
On Friday, 11 May 2012 at 15:00:18 UTC, Paul wrote:
> I would like to read a complete file in one statement and then
> process it line by line.
>
> foreach (line; MyFile)
> etc.
>
> Is it possible to read a file into and array of lines?
> Thanks
If you use the "byLine" approach...
foreach(line; File("myfile").byLine)) { ... }
or
File("myfile").byLine.doSomethingWithLines()
...keep in mind that "byLine", as it traverses your file, reuses
a single buffer to store the current line in. If you're storing
those lines somewhere for later use, use "line.dup" or
"line.idup" to make a copy of that buffer. (Or use a different
approach, like readText/splitLines.)
That was one of the first "gotcha" moments I had with D. :)
Graham
More information about the Digitalmars-d-learn
mailing list