This is an old revision of the document!

Good text I/O practice

The Ada standard libraries provides a general purpose text I/O package named Ada.Text_IO. This is good for small problems where efficiency is not an issue, and its parsing and formatting routines are excellent. One of the challenges for the compiler writers is that Ada.Text_IO is required to keep track of column, line and page numbers. Another challenge in the Ada.Text_IO design is that it requires “many” system calls or copying of the data being read/written from/to a file.

Below are some patterns for good text I/O practice. The tasks they solve are derived from Rosetta Code.

Read entire file

Ada.Direct_IO + Ada.Directories

Using Ada.Directories to first ask for the file size and then Ada.Direct_IO to read the whole file in one chunk:

with Ada.Directories,
procedure Read_Entire_File is
   File_Name : String  := "read_entire_file.adb";
   File_Size : Natural := Natural (Ada.Directories.Size (File_Name));
   subtype File_String    is String (1 .. File_Size);
   package File_String_IO is new Ada.Direct_IO (File_String);
   File     : File_String_IO.File_Type;
   Contents : File_String;
   File_String_IO.Open  (File, Mode => File_String_IO.In_File,
                               Name => File_Name);
   File_String_IO.Read  (File, Item => Contents);
   File_String_IO.Close (File);
   Ada.Text_IO.Put (Contents);
end Read_Entire_File;

This kind of solution is limited a bit by the fact that the GNAT implementation of Ada.Direct_IO first allocates a copy of the read object on the stack inside Ada.Direct_IO.Read. On Linux you can use the command

limit stacksize 1024M

to increase the available stack for your processes to 1Gb, which gives your program more freedom to use the stack for allocating objects.

This solution requires the Ada 2005 standard library (specificly package Ada.Directories) to work.


Mapping the whole file into the address space of your process and then overlaying the file with a String object.

with Ada.Text_IO,
procedure Read_Entire_File is
   use POSIX, POSIX.IO, POSIX.Memory_Mapping;
   use System.Storage_Elements;
   Text_File    : File_Descriptor;
   Text_Size    : System.Storage_Elements.Storage_Offset;
   Text_Address : System.Address;
   Text_File := Open (Name => "read_entire_file.adb",
                      Mode => Read_Only);
   Text_Size := Storage_Offset (File_Size (Text_File));
   Text_Address := Map_Memory (Length     => Text_Size,
                               Protection => Allow_Read,
                               Mapping    => Map_Shared,
                               File       => Text_File,
                               Offset     => 0);
      Text : String (1 .. Natural (Text_Size));
      for Text'Address use Text_Address;
      Ada.Text_IO.Put (Text);
   Unmap_Memory (First  => Text_Address,
                 Length => Text_Size);
   Close (File => Text_File);
end Read_Entire_File;

This solution requires the POSIX Ada API (implemented as FLORIST or WPOSIX) to work. (It has not been tested with an Ada 83 compiler.)


Using POSIX.Memory_Mapping is slightly faster than using Ada.Direct_IO, but you only really get a benefit from using memory mapping if you don't actually need the whole file, as the operating system only will copy in the parts of the file actually accessed by the application.

Process text file

Task description. In other words, read a file into a variable (possibly only a part of the file at a time) and write it out to another file.

Line by line

This solution reads from the file one line at a time. One nice thing about this solution is that you easily can switch it to read from standard input - and possibly anything which your operating system considers a file.

with Ada.Text_IO; use Ada.Text_IO;
procedure Read_File_Line_By_Line is
   Read_From : constant String := "input.txt";
   Write_To  : constant String := "output.txt";
   Input, Output : File_Type;
   Open (File => Input,
         Mode => In_File,
         Name => Read_From);
   Create (File => Output,
           Mode => Out_File,
           Name => Write_To);
         Line : String := Get_Line (Input);
         -- You can process the contents of Line here.
         Put_Line (Output, Line);
   end loop;
   when Name_Error =>
      Put_Line (Standard_Error,
                "Incorrect file name.  Does '" & Read_From & "' exist?");
   when End_Error =>
      Close (Input);
      Close (Output);
end Read_File_Line_By_Line;

This solution requires the Ada 2005 standard library to work.