Ada sequential and direct I/O
last modified November 23, 2026
A text file stores characters, so a program that reads a number back must
convert it again. A binary file stores the internal representation of values
and records, with no conversion at either end. Ada provides three generic
packages for this: Ada.Sequential_IO writes a series of values of
one type, Ada.Direct_IO treats the file as an array of records
addressed by index, and Ada.Streams.Stream_IO exposes the raw
bytes and the 'Read and 'Write attributes. This
article uses all three.
Writing records sequentially
The generic package Ada.Sequential_IO is instantiated with the
type of the values to store. Every Write appends one value, and
the file is a sequence of records of that one type. The program below stores
three points, each with two integer components.
with Ada.Text_IO;
with Ada.Sequential_IO;
use Ada.Text_IO;
procedure Seq_Write is
type Point is record
X : Integer;
Y : Integer;
end record;
package Point_IO is new Ada.Sequential_IO (Point);
F : Point_IO.File_Type;
begin
Point_IO.Create (F, Point_IO.Out_File, "points.dat");
Point_IO.Write (F, (X => 1, Y => 2));
Point_IO.Write (F, (X => 3, Y => 4));
Point_IO.Write (F, (X => 5, Y => 6));
Point_IO.Close (F);
Put_Line ("three points written");
end Seq_Write;
The file type and the modes belong to the instance, not to
Ada.Text_IO, which is why they are written
Point_IO.File_Type and Point_IO.Out_File. Writing a
record stores both integers in their machine representation, so the file is not
readable as text. The size of each record is fixed by the type, which is what
allows the next value to start at a known offset.
$ gnatmake -gnat2022 -gnatwa seq_write.adb $ ./seq_write three points written
Reading them back
Sequential input uses the same instance. Read takes the next
record from the current position and advances it, and
End_Of_File says whether any record remains. No parsing is
involved: the stored bits are copied back into a Point.
with Ada.Text_IO;
with Ada.Sequential_IO;
use Ada.Text_IO;
procedure Seq_Read is
type Point is record
X : Integer;
Y : Integer;
end record;
package Point_IO is new Ada.Sequential_IO (Point);
F : Point_IO.File_Type;
P : Point;
N : Natural := 0;
begin
Point_IO.Open (F, Point_IO.In_File, "points.dat");
while not Point_IO.End_Of_File (F) loop
Point_IO.Read (F, P);
N := N + 1;
Put_Line (Natural'Image (N) & ": x =" & Integer'Image (P.X)
& ", y =" & Integer'Image (P.Y));
end loop;
Point_IO.Close (F);
Put_Line ("records =" & Natural'Image (N));
end Seq_Read;
The loop reads exactly the three records written by the previous program and
then stops. Natural'Image (N) begins with a space for the
non-negative values, which is visible at the start of each line. A record
written by a different type, or by a program compiled with a different record
layout, would not read back correctly; binary files are tied to the types that
created them.
$ gnatmake -gnat2022 -gnatwa seq_read.adb $ ./seq_read 1: x = 1, y = 2 2: x = 3, y = 4 3: x = 5, y = 6 records = 3
Direct access by index
Ada.Direct_IO also stores fixed-size records, but each one has a
position, counted from 1. The position type is
Positive_Count, so an integer index must be converted explicitly.
This makes it possible to read or write the third record without touching the
first two.
with Ada.Text_IO;
with Ada.Direct_IO;
use Ada.Text_IO;
procedure Direct_Demo is
type Slot is record
Code : Integer;
Stock : Integer;
end record;
package Slot_IO is new Ada.Direct_IO (Slot);
F : Slot_IO.File_Type;
S : Slot;
begin
Slot_IO.Create (F, Slot_IO.Out_File, "slots.dat");
for I in 1 .. 5 loop
Slot_IO.Write (F, (Code => I * 10, Stock => I * 100),
Slot_IO.Positive_Count (I));
end loop;
Slot_IO.Close (F);
Slot_IO.Open (F, Slot_IO.In_File, "slots.dat");
Slot_IO.Read (F, S, 3);
Put_Line ("code 3 =" & Integer'Image (S.Code));
Put_Line ("stock 3 =" & Integer'Image (S.Stock));
Slot_IO.Close (F);
end Direct_Demo;
The write loop creates records at positions 1 through 5. The read asks for position 3 and receives the third record, whose code is 30 and stock is 300. Unlike a sequential file, a direct file can be read in any order, and the same position can be written more than once. The trade-off is that every record must have the same size and that the position must be tracked by the program.
$ gnatmake -gnat2022 -gnatwa direct_demo.adb $ ./direct_demo code 3 = 30 stock 3 = 300
Updating one record
Opening a direct file with Inout_File allows both reading and
writing. The usual update reads a record, changes a component in memory, and
writes the record back at the same position. Size returns the
number of records in the file.
with Ada.Text_IO;
with Ada.Direct_IO;
use Ada.Text_IO;
procedure Direct_Update is
type Slot is record
Code : Integer;
Stock : Integer;
end record;
package Slot_IO is new Ada.Direct_IO (Slot);
F : Slot_IO.File_Type;
S : Slot;
begin
Slot_IO.Create (F, Slot_IO.Out_File, "slots.dat");
for I in 1 .. 5 loop
Slot_IO.Write (F, (Code => I * 10, Stock => I * 100),
Slot_IO.Positive_Count (I));
end loop;
Slot_IO.Close (F);
Slot_IO.Open (F, Slot_IO.Inout_File, "slots.dat");
Slot_IO.Read (F, S, 2);
S.Stock := S.Stock + 5;
Slot_IO.Write (F, S, 2);
Put_Line ("slot 2 stock =" & Integer'Image (S.Stock));
Put_Line ("file size =" & Slot_IO.Count'Image (Slot_IO.Size (F)));
Slot_IO.Close (F);
end Direct_Update;
The record at position 2 is read, its stock is increased from 200 to 205, and
the whole record is written back. Omitting the second Write would
leave the file unchanged, because S is only a copy of the stored
record. Size returns a value of type Count, and its
image is printed after conversion, giving 5.
$ gnatmake -gnat2022 -gnatwa direct_update.adb $ ./direct_update slot 2 stock = 205 file size = 5
Byte streams
Ada.Streams.Stream_IO is the lowest of the three. A stream is a
sequence of bytes, and the attributes 'Write and
'Read convert a value to and from that sequence. The program
writes an integer, a real, and a Boolean, then reads them back in the same
order.
with Ada.Text_IO;
with Ada.Streams.Stream_IO;
use Ada.Text_IO;
procedure Stream_Demo is
package Stream_IO renames Ada.Streams.Stream_IO;
F : Stream_IO.File_Type;
S : Stream_IO.Stream_Access;
N : Integer := 42;
X : Float := 2.5;
Flag : Boolean := True;
begin
Stream_IO.Create (F, Stream_IO.Out_File, "values.bin");
S := Stream_IO.Stream (F);
Integer'Write (S, N);
Float'Write (S, X);
Boolean'Write (S, Flag);
Stream_IO.Close (F);
Stream_IO.Open (F, Stream_IO.In_File, "values.bin");
S := Stream_IO.Stream (F);
Integer'Read (S, N);
Float'Read (S, X);
Boolean'Read (S, Flag);
Stream_IO.Close (F);
Put_Line ("n =" & Integer'Image (N));
Put_Line ("x =" & Float'Image (X));
Put_Line ("flag =" & Boolean'Image (Flag));
end Stream_Demo;
The stream access value S is obtained from the file and used by
the attributes. Nothing records the types of the values, so the reader must
call the attributes in the same order and with the same types as the writer.
The output confirms that all three values survive the round trip. Streams are
the lowest-level of the three packages: the other two add a record size and a
position to the same bytes, and Text_IO adds character encoding
and line structure.
$ gnatmake -gnat2022 -gnatwa stream_demo.adb $ ./stream_demo n = 42 x = 2.50000E+00 flag =TRUE
An applied example: a fixed-size store
A direct file can serve as a simple fixed-size table. The program below creates four slots with empty names, stores two items, reduces the stock of one, and then lists the slots that are not empty. The empty name is a marker, and every name has the same length so that the record size stays constant.
with Ada.Text_IO;
with Ada.Direct_IO;
use Ada.Text_IO;
procedure Store_Demo is
type Item is record
Name : String (1 .. 8);
Stock : Integer;
end record;
package Item_IO is new Ada.Direct_IO (Item);
F : Item_IO.File_Type;
It : Item;
begin
Item_IO.Create (F, Item_IO.Out_File, "store.dat");
for I in 1 .. 4 loop
Item_IO.Write (F, (Name => "empty ", Stock => 0),
Item_IO.Positive_Count (I));
end loop;
Item_IO.Write (F, (Name => "hammer ", Stock => 12),
Item_IO.Positive_Count (1));
Item_IO.Write (F, (Name => "saw ", Stock => 5),
Item_IO.Positive_Count (3));
Item_IO.Close (F);
Item_IO.Open (F, Item_IO.Inout_File, "store.dat");
Item_IO.Read (F, It, Item_IO.Positive_Count (3));
It.Stock := It.Stock - 2;
Item_IO.Write (F, It, Item_IO.Positive_Count (3));
Item_IO.Close (F);
Item_IO.Open (F, Item_IO.In_File, "store.dat");
for I in 1 .. Item_IO.Size (F) loop
Item_IO.Read (F, It, Item_IO.Positive_Count (I));
if It.Name /= "empty " then
Put (It.Name);
Put_Line (Integer'Image (It.Stock));
end if;
end loop;
Item_IO.Close (F);
end Store_Demo;
The first loop initializes all four positions, so the file has a known size before anything is stored. The second pass overwrites positions 1 and 3, and the third pass updates position 3 in place. When the file is listed, positions 2 and 4 still contain the empty marker and are skipped. The output shows the two real items, with the saw's stock reduced from 5 to 3.
$ gnatmake -gnat2022 -gnatwa store_demo.adb $ ./store_demo hammer 12 saw 3
Next steps
The next article leaves the file system and reads the command line. It covers
Ada.Command_Line, the program name, the arguments, and the exit
status that a program returns to the shell.
Source
In this article we have written and read a sequential file of records,
addressed fixed-size records by index with Direct_IO, updated one
record in place, written and read raw values through a stream, and built a
small fixed-size store. The files article covers the
text files that these binary formats replace.
Author
List all Ada tutorials.