Read text file, line by line?

Jonathan M Davis jmdavisProg at gmx.com
Tue Dec 13 08:18:05 PST 2011


On Tuesday, December 13, 2011 14:08:14 Robert Clipsham wrote:
> On 13/12/2011 13:58, Iain S wrote:
> > How would one achieve this in D2?  I have tried for a couple of hours
> > now and have achieved nothing but stress.
> 
> import std.stdio;
> 
> void main() {
>      foreach(line; File("myTextFile.txt").byLine()) {
>          writefln(line);
>      }
> }

However, note that the buffer is reused between loop iterations, so if you're 
trying to keep any part of the line around after an iteration, you're going to 
need to dup or idup it.

- Jonathan M Davis


More information about the Digitalmars-d-learn mailing list