Read Complete File to Array of Lines

Graham Fawcett fawcett at uwindsor.ca
Fri May 11 12:24:48 PDT 2012


On Friday, 11 May 2012 at 18:57:52 UTC, Steven Schveighoffer 
wrote:
> On Fri, 11 May 2012 11:00:16 -0400, Paul <phshaffer at gmail.com> 
> 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
>
> Would something like this work?
>
> auto arr = array(map!"a.idup"(File("file.txt").byLine()));
>

It sure would. I suspect that Jesse's approach...

   readText("file.in").splitLines()

...would be the most efficient way if you need an actual array: 
slurp the whole file at once, then create an array of 
memory-sharing slices.

I look forward to the great std.stdio/std.file unification of 
201x, when I won't have to look in two modules for file-reading 
functions. :)

Graham



More information about the Digitalmars-d-learn mailing list