411 REFERENCE · Wikibooks
Ada Programming/Libraries/Ada.Sequential IO
Reference material by Wikibooks contributors. This retains its source identity and is not 411 news reporting.
Ada.Sequential_IO
__TOC__
Ada Sequential_IO is a unit of the Predefined Language Environment since Ada 95.
Background
Not all data read or written is in text format. For storage or inter-process communication, it is more space- and processing time-efficient to store data in binary format.
Binary input and output for sequential data (that is, read/written one after another) is managed by the generic Ada package Ada.Sequential_IO.
The generic package must be instantiated with some Element_Type, and provides a limited private File_Type.
The file mode, In_File or Out_File, is established on open or create.
The package provides the basic input/output (I/O) operations:
Open, Create
Close, Delete, and Reset
Read, Write
In addition, the package provides status and information functions:
Is_Open
End_Of_File
Name, Mode, and Form
One important restriction is that every element of a file must be the same element type.
Ada Code Example
For example, suppose that a student has collected information on trees in a 10 meter by 10 meter plot, and wishes a report generated from the data.
The following is a simple program to read the sequential data, previously recorded:
with Ada Text_IO; Text I/O library
with Ada Float_Text_IO; Float I/O library
with Ada Sequential_IO; Sequential I/O library
procedure Tree_Report is
First, define the contents of the data record
type Tree_Record is
record
Common_Name : String(1double_dot ..20) := (others arrow => ' ');
Scientific_Name : String(1double_dot ..20) := (others arrow => ' ');
Diameter : Float := 0dot .0; at breast height (DBH)
end record; Tree_Record
Now, instantiate the I/O package for the data record
package Tree_IO is new Adadot .Sequential_IO(Tree_Record);
Tree_File : Tree_IOdot .File_Type; Input data file
Tree_Data : Tree_Record; Individual data record
begin Tree_Report
Open the input data file.
Tree_IOdot .Open(File arrow => Tree_File,
Mode arrow => Tree_IOdot .In_File,
Name arrow => "tree.dat");
Write report heading
Adadot .Text_IOdot .Set_Col(To arrow => 1); Adadot .Text_IOdot .Put("COMMON NAME");
Adadot .Text_IOdot .Set_Col(To arrow => 21); Adadot .Text_IOdot .Put("SCIENTIFIC NAME");
Adadot .Text_IOdot .Set_Col(To arrow => 41); Adadot .Text_IOdot .Put("DBH");
Adadot .Text_IOdot .New_Line;
Process_Tree_Data:
while not Tree_IOdot .End_Of_File(Tree_File) loop
Tree_IOdot .Read(Tree_File, Tree_Data); Read tree data
Write tree inventory
Adadot .Text_IOdot .Put(Tree_Datadot .Common_Name);
Adadot .Text_IOdot .Put(Tree_Datadot .Scientific_Name);
Adadot .Float_Text_IOdot .Put(Tree_Datadot .Diameter,Forearrow =>2,Aftarrow =>2,Exparrow =>0);
Adadot .Text_IOdot .New_Line;
end loop Process_Tree_Data;
All data records have been read, and end_of_file detected.
Tree_IOdot .Close(Tree_File); Done, close file
end Tree_Report;
Specification
Standard Ada library specification
Copyright (c) 2003-2018 Maxim Reznik <reznikmm@gmail.com>
Copyright (c) 2004-2016 AXE Consultants
Copyright (c) 2004, 2005, 2006 Ada-Europe
Copyright (c) 2000 The MITRE Corporation, Inc.
Copyright (c) 1992, 1993, 1994, 1995 Intermetrics, Inc.
SPDX-License-Identifier: BSD-3-Clause and LicenseRef-AdaReferenceManual
-------------------------------------------------------------------------
with Ada IO_Exceptions;
generic
type Element_Type (box <>) is private;
package Ada Sequential_IO is
type File_Type is limited private;
type File_Mode is (In_File, Out_File, Append_File);
File management
procedure Create (File : in out File_Type;
Mode : in File_Mode := Out_File;
Name : in String := "";
Form : in String := "");
procedure Open (File : in out File_Type;
Mode : in File_Mode;
Name : in String;
Form : in String := "");
procedure Close (File : in out File_Type);
procedure Delete (File : in out File_Type);
procedure Reset (File : in out File_Type; Mode : in File_Mode);
procedure Reset (File : in out File_Type);
function Mode (File : in File_Type) return File_Mode;
function Name (File : in File_Type) return String;
function Form (File : in File_Type) return String;
function Is_Open (File : in File_Type) return Boolean;
Input and output operations
procedure Read (File : in File_Type; Item : out Element_Type);
procedure Write (File : in File_Type; Item : in Element_Type);
function End_Of_File (File : in File_Type) return Boolean;
Exceptions
Status_Error : exception renames IO_Exceptionsdot .Status_Error;
Mode_Error : exception renames IO_Exceptionsdot .Mode_Error;
Name_Error : exception renames IO_Exceptionsdot .Name_Error;
Use_Error : exception renames IO_Exceptionsdot .Use_Error;
Device_Error : exception renames IO_Exceptionsdot .Device_Error;
End_Error : exception renames IO_Exceptionsdot .End_Error;
Data_Error : exception renames IO_Exceptionsdot .Data_Error;
private
type File_Type is limited null record;
end Adadot .Sequential_IO;
See also
Wikibook
Ada Programming/Libraries/Ada
Ada Reference Manual
Ada 95
A 8 1 The Generic Package Sequential_IO
Ada 2005
A 8 1 The Generic Package Sequential_IO
Ada 2012
A 8 1 The Generic Package Sequential_IO
Open-Source Implementations
a-sequio.ads a-sequio.adb
streams/a-sequio.ads streams/a-sequio.adb