scope file.open
nobody
not at possible.de
Tue Jan 23 23:53:09 PST 2007
Chris Nicholson-Sauls Wrote:
> 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
The question is, how scope know it's a failure to open a file, before the file.open statement
is executed.
That is i didn't understand.
For this reason, i will put the scope(failure) after the file.open statement.
Thanks
More information about the Digitalmars-d-learn
mailing list