Basic file i/o

Stewart Gordon smjg_1998 at yahoo.com
Fri Sep 11 15:39:26 PDT 2009


Ali Cehreli wrote:
> What is the simplest way of using a file?
> 
> There are two 'File's in Phobos and they conflict:
> 
> 1) struct File in std.stdio
> 2) class File in std.stream

That's crazy.

> The one in std.stream.File is definitely what I want to use. What to do?

Import std.stream, create a File object for each file you want to read 
or write, and use its methods.

> Prefer using full names in D as in std.stream.File?

You can put in your own module

     alias std.stream.File File;

which'll mean that, whenever an unqualified File is used in the module 
where the declaration is placed, it means that File.

> Perhaps std.stdio is too low-level and too C-like, and I should not use it anyway?

It's good for simple console I/O (indeed, it's where you get writef), 
but I guess not much else.

> Should I be using std.cstream instead, as it defines din and dout?

You could indeed.  I sometimes use din/dout myself.  Other times I use 
the console I/O in my utility library
http://pr.stewartsplace.org.uk/d/sutil/

HTH

Stewart.


More information about the Digitalmars-d-learn mailing list