A.12 Stream Input-Output
This Reference Manual output has not been verified, and may contain omissions or errors. Report any problems on the tracking issue
The packages Streams.Stream_IO, Text_IO.Text_Streams, Wide_Text_IO.Text_Streams, and Wide_Wide_Text_IO.Text_Streams provide stream-oriented operations on files.
Wording Changes from Ada 95
A.12.1 The Package Streams.Stream_IO
1[The subprograms in the child package Streams.Stream_IO provide control over stream files. Access to a stream file is either sequential, via a call on Read or Write to transfer an array of stream elements, or positional (if supported by the implementation for the given file), by specifying a relative index for an element. Since a stream file can be converted to a Stream_Access value, calling stream-oriented attribute subprograms of different element types with the same Stream_Access value provides heterogeneous input-output.] See 13.13 for a general discussion of streams.
Static Semantics
1.1/1{8652/0055} The elements of a stream file are stream elements. If positioning is supported for the specified external file, a current index and current size are maintained for the file as described in A.8. If positioning is not supported, a current index is not maintained, and the current size is implementation defined.
The library package Streams.Stream_IO has the following declaration:
with Ada.IO_Exceptions;
package Ada.Streams.Stream_IO
with Preelaborate, Global => in out synchronized is
4type Stream_Access is access all Root_Stream_Type'Class;
5/5type File_Type is limited private
with Preelaborable_Initialization ;
6type File_Mode is (In_File, Out_File, Append_File);
7type Count is range 0 .. implementation-defined;
subtype Positive_Count is Count range 1 .. Count'Last;
-- Index into file, in stream elements.
8procedure Create (File : in out File_Type;
Mode : in File_Mode := Out_File;
Name : in String := "";
Form : in String := "");
9procedure Open (File : in out File_Type;
Mode : in File_Mode;
Name : in String;
Form : in String := "");
10procedure 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);
11function Mode (File : in File_Type) return File_Mode;
function Name (File : in File_Type) return String;
function Form (File : in File_Type) return String;
12function Is_Open (File : in File_Type) return Boolean;
function End_Of_File (File : in File_Type) return Boolean;
13function Stream (File : in File_Type) return Stream_Access;
-- Return stream access for use with T'Input and T'Output
14/1This paragraph was deleted.
15/5-- Read array of stream elements from file
procedure Read (File : in File_Type;
Item : out Stream_Element_Array;
Last : out Stream_Element_Offset;
From : in Positive_Count)
with Global => overriding in out File;
16/5procedure Read (File : in File_Type;
Item : out Stream_Element_Array;
Last : out Stream_Element_Offset)
with Global => overriding in out File;
17/1This paragraph was deleted.
18/5-- Write array of stream elements into file
procedure Write (File : in File_Type;
Item : in Stream_Element_Array;
To : in Positive_Count)
with Global => overriding in out File;
19/5procedure Write (File : in File_Type;
Item : in Stream_Element_Array)
with Global => overriding in out File;
20/1This paragraph was deleted.
21-- Operations on position within file
22/5procedure Set_Index(File : in File_Type; To : in Positive_Count)
with Global => overriding in out File;
23function Index(File : in File_Type) return Positive_Count;
function Size (File : in File_Type) return Count;
24procedure Set_Mode(File : in out File_Type; Mode : in File_Mode);
25/1{8652/0051} procedure Flush(File : in File_Type);
26-- 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;
26.1/5package Wide_File_Names is
26.2/5-- File management
26.3/5procedure Create (File : in out File_Type;
Mode : in File_Mode := Out_File;
Name : in Wide_String := "";
Form : in Wide_String := "");
26.4/5procedure Open (File : in out File_Type;
Mode : in File_Mode;
Name : in Wide_String;
Form : in Wide_String := "");
26.5/5function Name (File : in File_Type) return Wide_String;
26.6/5function Form (File : in File_Type) return Wide_String;
26.7/5end Wide_File_Names;
26.8/5package Wide_Wide_File_Names is
26.9/5-- File management
26.10/5procedure Create (File : in out File_Type;
Mode : in File_Mode := Out_File;
Name : in Wide_Wide_String := "";
Form : in Wide_Wide_String := "");
26.11/5procedure Open (File : in out File_Type;
Mode : in File_Mode;
Name : in Wide_Wide_String;
Form : in Wide_Wide_String := "");
26.12/5function Name (File : in File_Type) return Wide_Wide_String;
26.13/5function Form (File : in File_Type) return Wide_Wide_String;
26.14/5end Wide_Wide_File_Names;
27private
... -- not specified by the language
end Ada.Streams.Stream_IO;
27.1/2The type File_Type needs finalization (see 7.6).
The subprograms given in A.8.2 for the control of external files (Create, Open, Close, Delete, Reset, Mode, Name, Form, Is_Open, and Flush) are available for stream files.
The End_Of_File function:
- Propagates Mode_Error if the mode of the file is not In_File;
- If positioning is supported for the given external file, the function returns True if the current index exceeds the size of the external file; otherwise, it returns False;
- If positioning is not supported for the given external file, the function returns True if no more elements can be read from the given file; otherwise, it returns False.
{8652/0055} The Set_Mode procedure sets the mode of the file. If the new mode is Append_File, the file is positioned to its end; otherwise, the position in the file is unchanged.
This paragraph was deleted.{8652/0055}
{8652/0056} The Stream function returns a Stream_Access result from a File_Type object, thus allowing the stream-oriented attributes Read, Write, Input, and Output to be used on the same file for multiple types. Stream propagates Status_Error if File is not open.
The procedures Read and Write are equivalent to the corresponding operations in the package Streams. Read propagates Mode_Error if the mode of File is not In_File. Write propagates Mode_Error if the mode of File is not Out_File or Append_File. The Read procedure with a Positive_Count parameter starts reading at the specified index. The Write procedure with a Positive_Count parameter starts writing at the specified index. For a file that supports positioning, Read without a Positive_Count parameter starts reading at the current index, and Write without a Positive_Count parameter starts writing at the current index.
{8652/0055} The Size function returns the current size of the file.
{8652/0055} The Index function returns the current index.
The Set_Index procedure sets the current index to the specified value.
{8652/0055} If positioning is supported for the external file, the current index is maintained as follows:
- {8652/0055} For Open and Create, if the Mode parameter is Append_File, the current index is set to the current size of the file plus one; otherwise, the current index is set to one.
- {8652/0055} For Reset, if the Mode parameter is Append_File, or no Mode parameter is given and the current mode is Append_File, the current index is set to the current size of the file plus one; otherwise, the current index is set to one.
- {8652/0055} For Set_Mode, if the new mode is Append_File, the current index is set to current size plus one; otherwise, the current index is unchanged.
- {8652/0055} For Read and Write without a Positive_Count parameter, the current index is incremented by the number of stream elements read or written.
- {8652/0055} For Read and Write with a Positive_Count parameter, the value of the current index is set to the value of the Positive_Count parameter plus the number of stream elements read or written.
If positioning is not supported for the given file, then a call of Index or Set_Index propagates Use_Error. Similarly, a call of Read or Write with a Positive_Count parameter propagates Use_Error.
Paragraphs 34 through 36 were deleted.
Erroneous Execution
36.1/1{8652/0056} If the File_Type object passed to the Stream function is later closed or finalized, and the stream-oriented attributes are subsequently called (explicitly or implicitly) on the Stream_Access value returned by Stream, execution is erroneous. This rule applies even if the File_Type object was opened again after it had been closed.
Inconsistencies With Ada 95
Incompatibilities With Ada 95
Wording Changes from Ada 95
Extensions to Ada 2005
Incompatibilities With Ada 2012
Extensions to Ada 2012
Wording Changes from Ada 2012
A.12.2 The Package Text_IO.Text_Streams
1The package Text_IO.Text_Streams provides a function for treating a text file as a stream.
Static Semantics
2The library package Text_IO.Text_Streams has the following declaration:
with Ada.Streams;
package Ada.Text_IO.Text_Streams
with Global => in out synchronized is
type Stream_Access is access all Streams.Root_Stream_Type'Class;
4function Stream (File : in File_Type) return Stream_Access;
end Ada.Text_IO.Text_Streams;
5The Stream function has the same effect as the corresponding function in Streams.Stream_IO.
A.12.3 The Package Wide_Text_IO.Text_Streams
1The package Wide_Text_IO.Text_Streams provides a function for treating a wide text file as a stream.
Static Semantics
2The library package Wide_Text_IO.Text_Streams has the following declaration:
with Ada.Streams;
package Ada.Wide_Text_IO.Text_Streams
with Global => in out synchronized is
type Stream_Access is access all Streams.Root_Stream_Type'Class;
4function Stream (File : in File_Type) return Stream_Access;
end Ada.Wide_Text_IO.Text_Streams;
5The Stream function has the same effect as the corresponding function in Streams.Stream_IO.
A.12.4 The Package Wide_Wide_Text_IO.Text_Streams
1/2The package Wide_Wide_Text_IO.Text_Streams provides a function for treating a wide wide text file as a stream.
Static Semantics
2/2The library package Wide_Wide_Text_IO.Text_Streams has the following declaration:
with Ada.Streams;
package Ada.Wide_Wide_Text_IO.Text_Streams
with Global => in out synchronized is
type Stream_Access is access all Streams.Root_Stream_Type'Class;
4/2function Stream (File : in File_Type) return Stream_Access;
end Ada.Wide_Wide_Text_IO.Text_Streams;
5/2The Stream function has the same effect as the corresponding function in Streams.Stream_IO.