Ada Programming/Libraries/Ada.Sequential_IO
This language feature is available from Ada 95 on.
Ada.Sequential_IO is a unit of the Predefined Language Environment since Ada 95.
Background
editNot 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
, andReset
Read
,Write
In addition, the package provides status and information functions:
Is_Open
End_Of_File
Name
,Mode
, andForm
One important restriction is that every element of a file must be the same element type.
Ada Code Example
editFor 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 librarywith
Ada.Float_Text_IO; -- Float I/O librarywith
Ada.Sequential_IO; -- Sequential I/O libraryprocedure
Tree_Reportis
-- First, define the contents of the data recordtype
Tree_Recordis
record
Common_Name : String(1..20) := (others
=> ' '); Scientific_Name : String(1..20) := (others
=> ' '); Diameter : Float := 0.0; -- at breast height (DBH)end
record
; -- Tree_Record -- Now, instantiate the I/O package for the data recordpackage
Tree_IOis
new
Ada.Sequential_IO(Tree_Record); Tree_File : Tree_IO.File_Type; -- Input data file Tree_Data : Tree_Record; -- Individual data recordbegin
-- Tree_Report -- Open the input data file. Tree_IO.Open(File => Tree_File, Mode => Tree_IO.In_File, Name => "tree.dat"); -- Write report heading Ada.Text_IO.Set_Col(To => 1); Ada.Text_IO.Put("COMMON NAME"); Ada.Text_IO.Set_Col(To => 21); Ada.Text_IO.Put("SCIENTIFIC NAME"); Ada.Text_IO.Set_Col(To => 41); Ada.Text_IO.Put("DBH"); Ada.Text_IO.New_Line; Process_Tree_Data:while
not
Tree_IO.End_Of_File(Tree_File)loop
Tree_IO.Read(Tree_File, Tree_Data); -- Read tree data -- Write tree inventory Ada.Text_IO.Put(Tree_Data.Common_Name); Ada.Text_IO.Put(Tree_Data.Scientific_Name); Ada.Float_Text_IO.Put(Tree_Data.Diameter,Fore=>2,Aft=>2,Exp=>0); Ada.Text_IO.New_Line;end
loop
Process_Tree_Data; -- All data records have been read, and end_of_file detected. Tree_IO.Close(Tree_File); -- Done, close fileend
Tree_Report;
Specification
edit-- 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 (<>)is
private
;package
Ada.Sequential_IOis
type
File_Typeis
limited
private
;type
File_Modeis
(In_File, Out_File, Append_File); -- File managementprocedure
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 operationsprocedure
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_Exceptions.Status_Error; Mode_Error :exception
renames
IO_Exceptions.Mode_Error; Name_Error :exception
renames
IO_Exceptions.Name_Error; Use_Error :exception
renames
IO_Exceptions.Use_Error; Device_Error :exception
renames
IO_Exceptions.Device_Error; End_Error :exception
renames
IO_Exceptions.End_Error; Data_Error :exception
renames
IO_Exceptions.Data_Error;private
type
File_Typeis
limited
null
record
;end
Ada.Sequential_IO;
See also
editWikibook
editExternal examples
edit- Search for examples of
Ada.Sequential_IO
in: Rosetta Code, GitHub (gists), any Alire crate or this Wikibook. - Search for posts related to
Ada.Sequential_IO
in: Stack Overflow, comp.lang.ada or any Ada related page.
Ada Reference Manual
editAda 95
editAda 2005
editAda 2012
editOpen-Source Implementations
editFSF GNAT
- Specification: a-sequio.ads
- Body: a-sequio.adb
drake
- Specification: streams/a-sequio.ads
- Body: streams/a-sequio.adb