std.file vs

Ludovic A. ludovica at
Thu Mar 4 06:16:57 PST 2010

I am trying to write a simple file parser in D2. Following examples I found 2
working ways to read a line and print it:

  Stream file = new BufferedFile(filename);
  foreach(char[] line; file)
    writefln("line: %s",line);


import std.file;
File* file = new File(filename);
   while (!file.eof())
     writef("%s", file.readln());

My question from there is what is the difference between opening a stream and
a file _in this case_, and what is the most appropriate in this case?

Also in the foreach I can not write foreach (string line, file) but I have to
use char[]. Is there a way to go around this?

Finally I can not use foreach in the std.file example, because the compiler
says "foreach: File* is not an aggregate type". I was surprised that foreach
worked on a Stream at the first place, so no big deal :) But what do we have
under the hood? How can it work for Stream and not for File? Is the limitation
coming from the foreach implementation?

More information about the Digitalmars-d-learn mailing list