Read text file, line by line?

David Medlock noone at nowhere.com
Sat Mar 3 07:09:25 PST 2007


AEon wrote:
> It seems *very* easy to open a text file in D, see below code (snipped from the
> wordcount example from the documentation).
> 
> <code>
>    import std.file;
>    // Open/read complete file into a D string! Nice.	
>    char[] input;
>    input = cast(char[])std.file.read(arg);		
> </code>
> 
> I checked the other command in std.file. AFAICT there seems to be no way to open
> a file handle, and then read *line by line* from a e.g. config file?
> 
> Does one need to parse the file as one char[] array block, and do all the line
> by line checking by hand?
> 
> AEon

Is this what you need?



import std.stream;

auto x = new File( filename, FileMode.In );
scope(exit) { x.close(); }
foreach( char[] line; x ) writefln( line );

-DavidM



More information about the Digitalmars-d-learn mailing list