Read Complete File to Array of Lines

Paul phshaffer at gmail.com
Fri May 11 13:37:07 PDT 2012


On Friday, 11 May 2012 at 19:24:49 UTC, Graham Fawcett wrote:
> 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
GULP!  This language is so powerful!  Do I dare ask a followup 
question?
I will try the readText.splitLines().  Thanks to all!




More information about the Digitalmars-d-learn mailing list