This is an old revision of the document!


File_Exists considered harmful?

The function File_Exists (actually Ada.Directories.Exists) should generally be avoided. Instead one should just get on with whatever business one has with the file in question and handle the exceptions that may be raised.

The reason for this is that you (practically) never can be sure that another program doesn't mess with the same file as your program at the same time.

A brainstorm including comp.lang.ada regulars, #Ada (IRC channel on Freenode) and AdaHeads staff has so far only resulted in two use cases for File_Exists.

Open or create file

This procedure will first attempt to open a file with the given name in append mode. If it succeeds all is well. If it doesn't succeed it assumes that the file was missing and attempts to create the file. If that also fails (for example because the file name is invalid) an exception will be raised.

procedure Open_Or_Create (File : in out File_Type;
                          Name : in     String) is
begin
   Open (File => File,
         Name => Name,
         Mode => Append_File);
exception
   when others =>
      Create (File => File,
              Name => Name,
              Mode => Append_File);
end Open_Or_Create;

Navigation