File I/O: Text, Sequential, Direct & Stream Files
Create, open, append, read and close text files; store typed binary records with Sequential_IO and Direct_IO (random access with indexes); write heterogeneous data with Stream_IO and the 'Write/'Read/'Output/'Input stream attributes; handle Name_Error, Use_Error, End_Error and Data_Error.
Ada offers four families of file I/O: Text_IO for human-readable text, Sequential_IO for a sequence of values of one type, Direct_IO for fixed-size records accessed by index, and Stream_IO for arbitrary mixtures of types via stream attributes. All share the same lifecycle: Create or Open with a mode, operate, Close.
Text Files
Declare 'F : File_Type;', then Create (F, Out_File, "notes.txt") to create/overwrite, Open (F, In_File | Out_File | Append_File, Name) for existing files, and Close (F). Every Text_IO operation has a version taking the file first: Put_Line (F, ...), Get_Line (F, ...), End_Of_File (F), Line (F) (the current line number). Text_IO writes the platform's line terminator — on Windows CR LF, so a file with 34 visible characters in 3 lines is 40 bytes. Is_Open (F) checks the state; Reset changes the mode; Delete removes the file.
• Create / Open (In_File, Out_File, Append_File) / Close. • All Text_IO operations accept a File_Type first. • Line terminators are platform-specific (CR LF on Windows).
Sequential_IO and Direct_IO
'package Reading_IO is new Ada.Sequential_IO (Reading);' creates binary I/O for one element type: Write (F, Item) and Read (F, Item) in order. 'package Int_Direct is new Ada.Direct_IO (Integer);' adds random access: files are arrays of records numbered from 1; Read (F, Item, From => 4), Write (F, Item, To => 2), Set_Index, Index and Size. Mode Inout_File allows mixing reads and writes. The binary layout is the in-memory representation, so such files are not portable between compilers or platforms.
• Sequential_IO (T): Read/Write in order. • Direct_IO (T): records 1 .. Size, Read From / Write To / Set_Index. • Binary files use the machine representation — not portable.
Streams
Ada.Streams.Stream_IO gives a Stream_Access for a file. Stream attributes then serialize any type: T'Write (S, X) writes the value, T'Read (S, X) reads it; T'Output (S, X) also writes the bounds/discriminants (and the tag for class-wide values), and T'Input (S) is a function that reads them back — essential for indefinite types like String or Shape'Class. You can redefine these attributes for your types (for T'Write use My_Write;) to control the external format. Streams also work over sockets and memory buffers.
• T'Write / T'Read — values only. • T'Output / T'Input — include bounds, discriminants, tags. • Customizable per type; usable with any stream (files, sockets).
I/O Exceptions
Ada.IO_Exceptions (renamed in each I/O package): Name_Error — file not found or bad name; Use_Error — operation not allowed (e.g. permissions, file already open); Mode_Error — reading an Out_File; Status_Error — file not open (or already open); End_Error — reading past the end; Data_Error — malformed data.
• Name_Error, Use_Error, Mode_Error, Status_Error, End_Error, Data_Error.
Code Examples
Text, sequential, direct and stream files are created, reread and then deleted. Buf is used as the buffer name because a variable called Line would hide Text_IO.Line. The size of notes.txt is from a Windows run (CR LF terminators). Output: text: first line (line 1) text: second line (line 2) text: appended line (line 3) sequential: sensor 1 = 2.05000E+01 sequential: sensor 2 = 2.17500E+01 direct: record 4 = 16, size = 5 direct: record 2 now = 1000 stream: 7, "Ada", sensor 3 = 1.50000E+00 notes.txt size: 40 bytes Open of missing.txt raised Name_Error
Open (F, Out_File, "log.txt"); -- expecting to add lines Open (F, Append_File, "log.txt");
Out_File truncates the file; Append_File adds to the end.
String'Write (S, Name); ... String'Read (S, Buf); -- unknown length
String'Output (S, Name); ... Name2 : constant String := String'Input (S);
'Output/'Input store and restore the bounds of indefinite types.
-- forgetting Close on every path -- close in handlers too, or wrap the file in a Limited_Controlled type
Unclosed files may lose buffered data; RAII wrappers guarantee closing.
Which mode adds to the end of an existing text file? A. Out_File B. Append_File C. Inout_File D. In_File
Which package reads record number 4 directly? A. Sequential_IO B. Direct_IO C. Text_IO D. Stream_IO only
B. Direct_IO with Read (F, Item, From => 4).
Which attribute writes a String including its bounds? A. String'Write B. String'Output C. String'Image D. String'Put
Opening a file that doesn't exist raises... A. Use_Error B. Name_Error C. End_Error D. Constraint_Error B. Name_Error (verified).
Trace the file I/O example.
Real output: text: first line (line 1) text: second line (line 2) text: appended line (line 3) sequential: sensor 1 = 2.05000E+01 sequential: sensor 2 = 2.17500E+01 direct: record 4 = 16, size = 5 direct: record 2 now = 1000 stream: 7, "Ada", sensor 3 = 1.50000E+00 notes.txt size: 40 bytes Open of missing.txt raised Name_Error
with Ada.Text_IO;
with Ada.Sequential_IO;
with Ada.Direct_IO;
with Ada.Streams.Stream_IO;
with Ada.Directories;
procedure L46_Files is
use Ada.Text_IO;
type Reading is record
Sensor : Positive;
Value : Float;
end record;
package Reading_IO is new Ada.Sequential_IO (Reading);
package Int_Direct is new Ada.Direct_IO (Integer);
T : File_Type;
Buf : String (1 .. 100);
Last : Natural;
begin
-- ---------- Text files ----------
Create (T, Out_File, "notes.txt");
Put_Line (T, "first line");
Put_Line (T, "second line");
Close (T);
Open (T, Append_File, "notes.txt");
Put_Line (T, "appended line");
Close (T);
Open (T, In_File, "notes.txt");
while not End_Of_File (T) loop
Get_Line (T, Buf, Last);
Put_Line ("text: " & Buf (1 .. Last) & " (line" & Positive_Count'Image (Line (T) - 1) & ")");
end loop;
Close (T);
-- ---------- Sequential binary files ----------
declare
use Reading_IO;
F : Reading_IO.File_Type;
R : Reading;
begin
Create (F, Out_File, "readings.bin");
Write (F, (1, 20.5));
Write (F, (2, 21.75));
Close (F);
Open (F, In_File, "reading
…