Temporary Files and Directories in Ada

Our first example demonstrates how to work with temporary files and directories in Ada. This is useful when we need to create data that isn’t needed after the program exits.

with Ada.Text_IO;
with Ada.Directories;
with Ada.Strings.Unbounded;
with GNAT.OS_Lib;

procedure Temporary_Files_And_Directories is
   use Ada.Text_IO;
   use Ada.Directories;
   use Ada.Strings.Unbounded;

   procedure Check (Condition : Boolean) is
   begin
      if not Condition then
         raise Program_Error with "An error occurred";
      end if;
   end Check;

   Temp_File : File_Type;
   Temp_File_Name : Unbounded_String;
   Temp_Dir_Name : Unbounded_String;
begin
   -- Create a temporary file
   Create (Temp_File, Out_File, "");
   Temp_File_Name := To_Unbounded_String (Name (Temp_File));
   Put_Line ("Temp file name: " & To_String (Temp_File_Name));

   -- Write some data to the file
   Put (Temp_File, Character'Val (1));
   Put (Temp_File, Character'Val (2));
   Put (Temp_File, Character'Val (3));
   Put (Temp_File, Character'Val (4));

   -- Close the file
   Close (Temp_File);

   -- Create a temporary directory
   Temp_Dir_Name := To_Unbounded_String (Create_Temporary_Directory);
   Put_Line ("Temp dir name: " & To_String (Temp_Dir_Name));

   -- Create a file in the temporary directory
   declare
      File_Name : constant String := Compose (To_String (Temp_Dir_Name), "file1");
      File : File_Type;
   begin
      Create (File, Out_File, File_Name);
      Put (File, Character'Val (1));
      Put (File, Character'Val (2));
      Close (File);
   end;

   -- Clean up
   Check (Delete_File (To_String (Temp_File_Name)));
   Check (Delete_Tree (To_String (Temp_Dir_Name)));
end Temporary_Files_And_Directories;

In this Ada example, we use the Ada.Directories package to create temporary files and directories. Here’s a breakdown of what the code does:

  1. We create a temporary file using Create with an empty string as the name, which tells Ada to generate a unique name.

  2. We display the name of the temporary file. The exact location and name format may vary depending on the operating system.

  3. We write some data to the file using Put.

  4. We create a temporary directory using Create_Temporary_Directory.

  5. We create a file inside the temporary directory using Compose to join the directory path and the file name.

  6. Finally, we clean up by deleting the temporary file and directory. Note that in Ada, we need to explicitly delete these temporary items.

To run this program, save it as temporary_files_and_directories.adb and compile it with an Ada compiler like GNAT:

$ gnatmake temporary_files_and_directories.adb
$ ./temporary_files_and_directories
Temp file name: /tmp/XXXXXXXXXX
Temp dir name: /tmp/YYYYYYYYYY

The actual names of the temporary file and directory will be unique and may look different on your system.

Remember that while the operating system might eventually clean up temporary files and directories, it’s good practice to delete them explicitly in your program when you’re done using them.