scope file.open

Chris Nicholson-Sauls ibisbasenji at gmail.com
Tue Jan 23 12:31:45 PST 2007


nobody wrote:
> Hallo,
> 
> how can i saftly open a file with scope.
> My  idea is:
> import std.stream;
> import std.stdio;
> 
> void main() {
>         File file = new File;
> 
>         scope(failure) {
>                 writefln("Datei kann nicht geoeffnet werden ");
>         }
>         file.open("testdatei.txt",FileMode.In);
> 
>         while(!file.eof()) {
>                 printf("%.*s\n",file.readLine());
> 
>         }
>         file.close();
> }
> 
> 
> But why must scope(failure) before the file.open statement.
> I think it's look strange.
> Mybe some on have a better idea to open a file with scope.
> 
> Sincerily
> 

Personally I don't think it really looks all that strange, but I'm odd like that.  :)  In 
this case, if it worries you, you might try a traditional try-catch-finally instead.

# void main () {
#   File file = new File ;
#
#   try {
#     file.open("testdatei.txt",FileMode.In);
#     while(!file.eof()) {
#       printf("%.*s\n",file.readLine());
#     }
#   }
#   catch (Exception x) {
#     writefln("Datei kann nicht geoeffnet werden ");
#     throw x;
#   }
#
#   file.close();
# }

-- Chris Nicholson-Sauls


More information about the Digitalmars-d-learn mailing list