ZetCode

Ada files

last modified November 23, 2026

Text files use the same Ada.Text_IO operations as the standard input and output, with one extra argument: the file. A file is represented by an object of type File_Type, which the program creates or opens, uses, and closes. This article writes and reads lines, appends to an existing file, handles a missing file, inspects whether a file is open, and finishes with a program that writes a small report and reads it back.

Creating and writing a file

Create opens a file for output and empties it if it already exists. The second parameter gives the mode, here Out_File, and the third is the file name. Every Put_Line that takes the file as its first argument writes to that file instead of the screen.

write_file.adb
with Ada.Text_IO;
use Ada.Text_IO;

procedure Write_File is
   F : File_Type;
begin
   Create (F, Out_File, "notes.txt");
   Put_Line (F, "first line");
   Put_Line (F, "second line");
   Close (F);
   Put_Line ("file written");
end Write_File;

The name is relative to the directory in which the program runs, so notes.txt appears beside the executable unless the program is started from somewhere else. Close flushes the buffers and releases the file. Forgetting to close a file risks losing the last lines if the program terminates abnormally, and it also leaves the object in a state that Is_Open reports as closed.

$ gnatmake -gnat2022 -gnatwa write_file.adb
$ ./write_file
file written
$ cat notes.txt
first line
second line

Reading a file

Open with In_File opens an existing file for reading. End_Of_File accepts the file object and works exactly as it does for the standard input, and Get_Line takes the file as its first argument. The following program reads the file written above.

read_file.adb
with Ada.Text_IO;
use Ada.Text_IO;

procedure Read_File is
   F    : File_Type;
   Line : String (1 .. 100);
   Last : Natural;
begin
   Open (F, In_File, "notes.txt");
   while not End_Of_File (F) loop
      Get_Line (F, Line, Last);
      Put_Line (Line (1 .. Last));
   end loop;
   Close (F);
end Read_File;

The loop prints each line exactly as it was stored, without the newline that ended it, because Get_Line removes that character. The buffer holds at most 100 characters per line; a longer line would be split across two calls. Reading does not change the file, and the program does not need write permission to open it with In_File.

$ gnatmake -gnat2022 -gnatwa read_file.adb
$ ./read_file
first line
second line

Appending to a file

Append_File opens a file for writing but positions the output at its end, so existing content is preserved. The file must already exist: unlike Create, this mode does not make a new one, and opening a missing file raises Name_Error. The program below adds a third line to the file from the first section.

append_file.adb
with Ada.Text_IO;
use Ada.Text_IO;

procedure Append_File is
   F : File_Type;
begin
   Open (F, Append_File, "notes.txt");
   Put_Line (F, "third line");
   Close (F);
   Put_Line ("line appended");
end Append_File;

The file now holds three lines, in the order in which they were written. The difference from Out_File is important: opening with Out_File and then writing would have erased the first two lines. Appending is the right mode for a log or for adding records to a file that accumulates over several runs.

$ gnatmake -gnat2022 -gnatwa append_file.adb
$ ./append_file
line appended
$ cat notes.txt
first line
second line
third line

A missing file

Opening a file that does not exist raises Name_Error. The handler can report the problem and let the program continue with other work, which is usually better than letting the exception propagate and terminate the program. The file object is not left open when Open fails.

missing_file.adb
with Ada.Text_IO;
use Ada.Text_IO;

procedure Missing_File is
   F : File_Type;
begin
   Open (F, In_File, "missing.txt");
   Put_Line ("file opened");
   Close (F);
exception
   when Name_Error =>
      Put_Line ("no such file: missing.txt");
end Missing_File;

Name_Error covers a path that does not exist and a name that the system rejects. A related exception, Use_Error, reports an operation that the file's mode or the system does not allow, and Status_Error reports an operation on a file object that is not open. The handler here replaces the normal ending of the procedure, so Put_Line ("file opened") is not reached.

$ gnatmake -gnat2022 -gnatwa missing_file.adb
$ ./missing_file
no such file: missing.txt

File status and mode

Two functions describe a file object. Is_Open is true while the file is open, and Mode returns the current mode as a value of type File_Mode. The image of a mode is the name of its enumeration literal, such as OUT_FILE.

file_status.adb
with Ada.Text_IO;
use Ada.Text_IO;

procedure File_Status is
   F : File_Type;
begin
   Create (F, Out_File, "status.txt");
   Put_Line (F, "data");
   Put_Line ("open =" & Boolean'Image (Is_Open (F)));
   Put_Line ("mode =" & File_Mode'Image (Mode (F)));
   Close (F);
   Put_Line ("open =" & Boolean'Image (Is_Open (F)));
end File_Status;

The mode is read while the file is open, because a closed file has no mode to report: calling Mode (F) after Close raises Status_Error. Is_Open is safe at any time and is useful in cleanup code that must close a file only if it was opened. The output shows that the object is closed again after Close, even though the file itself remains on disk.

$ gnatmake -gnat2022 -gnatwa file_status.adb
$ ./file_status
open =TRUE
mode =OUT_FILE
open =FALSE

An applied example: writing and reading a report

The final program writes an array of records to a file, closes it, and opens it again to print what was stored. The name has a fixed width of seven characters, and the count is written in six columns, so the two fields line up without any parsing when the file is read back.

report_file.adb
with Ada.Text_IO;
use Ada.Text_IO;

procedure Report_File is
   package Int_IO is new Ada.Text_IO.Integer_IO (Integer);
   use Int_IO;

   type Row is record
      Name  : String (1 .. 7);
      Count : Integer;
   end record;

   Rows : constant array (1 .. 3) of Row :=
     [(Name => "bolts  ", Count => 120),
      (Name => "nuts   ", Count => 340),
      (Name => "washers", Count => 75)];

   F    : File_Type;
   Line : String (1 .. 80);
   Last : Natural;
begin
   Create (F, Out_File, "stock.txt");
   for I in Rows'Range loop
      Put (F, Rows (I).Name);
      Put (F, Rows (I).Count, Width => 6);
      New_Line (F);
   end loop;
   Close (F);

   Open (F, In_File, "stock.txt");
   Put_Line ("--- stock.txt ---");
   while not End_Of_File (F) loop
      Get_Line (F, Line, Last);
      Put_Line (Line (1 .. Last));
   end loop;
   Close (F);
end Report_File;

The writing loop produces three lines of 13 characters each. The reading loop does not know that structure; it simply prints every line, which is the advantage of text files. A program on the other side could slice the name and call Integer'Value on the number, or use Get after skipping the name. The next article writes records in binary form instead, so that no conversion is needed at all.

$ gnatmake -gnat2022 -gnatwa report_file.adb
$ ./report_file
--- stock.txt ---
bolts     120
nuts      340
washers    75

Next steps

The next article covers binary files. Ada.Sequential_IO writes records one after another, Ada.Direct_IO addresses them by index, and Ada.Streams.Stream_IO reads and writes raw values without any text conversion.

Source

Ada Reference Manual

GNAT User's Guide

AdaCore learning resources

In this article we have created and written a text file, read it line by line, appended a line without erasing the earlier ones, caught Name_Error for a missing file, checked Is_Open and Mode, and written and read back a small formatted report. The reading input article explains the line reading used here in more detail.

Author

I am Jan Bodnar, a programmer and technical writer. I have been writing programming tutorials at ZetCode since 2007 and have published eight e-books. See about the author.

List all Ada tutorials.