File Ex from std_stdio won't compile

simendsjo simendsjo at gmail.com
Wed Apr 18 05:42:26 PDT 2012


On Wed, 18 Apr 2012 14:09:20 +0200, Paul <phshaffer at gmail.com> wrote:

> I copied the code below from this site under the std.stdio library  
> reference section and tried to compile it.  I downloaded D2/Phobos.   
> Compiler says "fileread.d(4): Error: undefined identifier File"
>
> If I add import std.stdio to the beginning it get ; expected, function  
> declaration w/o return type, etc.
>
> Assistance is greatly appreciated.
>
>   void main(string args[])
> {
>      auto f = File("test.txt", "w"); // open for writing
>      f.write("Hello");
>      if (args.length > 1)
>      {
>          auto g = f; // now g and f write to the same file
>                      // internal reference count is 2
>          g.write(", ", args[1]);
>          // g exits scope, reference count decreases to 1
>      }
>      f.writeln("!");
>      // f exits scope, reference count falls to zero,
>      // underlying FILE* is closed.
> }

As you note, File is a part of stdio. I think you might have forgotten to  
terminate the import statement with a semicolon.
The following should work:

import std.stdio;
void main() {
     auto f = File("test.txt", "w");
     f.write("Hello");
}


More information about the Digitalmars-d-learn mailing list