std.file vs std.stream

Ali Çehreli acehreli at yahoo.com
Thu Mar 4 12:19:56 PST 2010


Lars T. Kyllingstad wrote:

 > What you're seeing here is actually a consequence of Phobos very much
 > being a work-in-progress.  std.stdio was completely rewritten about a
 > year ago, and it most likely contains what you're looking for.
 >
 > std.stream, on the other hand, hasn't seen major updates since 2005 or
 > so, and is most likely an old left-over from D1.  I suspect it will be
 > removed soon, or at least completely rewritten to adhere to the range
 > interface.

I've been using std.stream.File because it uses the generic Stream 
interface like dout and din does.

Using std.stream.File should allow cleaner template code without needing 
'static if' and other conditional compilation features.

 > So, my advice is to use std.stdio.File, and to use it like this:
 >
 >   import std.stdio;
 >   ...
 >   auto file = File(filename);
 >   foreach (line; file.byLine)
 >   {
 >       writeln(line);
 >   }

std.stdio.File has two advantages that I've discovered so far:

- As you also note, it closes the file immediately. With 
std.stream.File, I have to use 'scope' objects

- It allows chosing text vs. binary file I/O mode. 
std.stream.Stream.writefln always converts the newline character 
according to the system

Ali


More information about the Digitalmars-d-learn mailing list