Ada Programming/Input Output


Ada Lovelace 1838.jpg

OverviewEdit

The standard Ada libraries provide several Input/Output facilities, each one adapted to specific needs. Namely, the language defines the following dedicated packages:

  • Text_IO
  • Sequential_IO
  • Direct_IO
  • Stream_IO

The programmer must choose the adequate package depending on the application needs. For example, the following properties of the data handled by the application should be considered:

  • Data contents: plain text, or binary data?
  • Accessing the data: random access, or sequential access?
  • Medium: data file, console, network/data-bus?
  • Data structure: homogeneous file (sequence of the same data field), heterogeneous file (different data fields)?
  • Data format: adherence to an existing data format, or the application can freely choose a new one?

For example, Stream_IO is very powerful and can handle complex data structures but can be heavier than other packages; Sequential_IO is lean and easy to use but cannot be used by applications requiring random data access; Text_IO can handle just textual data, but it is enough for handling the command-line console.


The following table gives some advices for choosing the more adequate one:

Simple heuristics for choosing an I/O package
Data access Plain text Binary data
Homogeneous Heterogeneous
Sequential Text_IO Sequential_IO Stream_IO
Random Text_IO Direct_IO Stream_IO

So the most important lesson to learn is choosing the right one. This chapter will describe more in detail these standard packages, explaining how to use them effectively. Besides these Ada-defined packages for general I/O operations each Ada compiler usually has other implementation-defined Input-Output facilities, and there are also other external libraries for specialized I/O needs like XML processing or interfacing with databases.

Text I/OEdit

Text I/O is probably the most used Input/Output package. All data inside the file are represented by human readable text. Text I/O provides support for line and page layout but the standard is free form text.

with Ada.Strings;
use  Ada.Strings;
with Ada.Text_IO;
use  Ada.Text_IO;
 
procedure Main is
  Str  : String (1..5);
  Last : Natural;
begin
  Ada.Text_IO.Get_Line (Str, Last);
  Ada.Text_IO.Put_Line (Str (1..Last));
end;

It also contains several generic packages for converting numeric and enumeration types to character strings, or for handling Bounded and Unbounded strings, allowing the programmer to read and write different data types in the same file easily (there are ready-to-use instantiations of these generic packages for the Integer, Float, and Complex types). Finally, the same family of Ada.Text_IO packages (including the several children and instantiation packages) for the type Wide_Character and Wide_Wide_Character.

It is worth noting that the family of Text_IO packages provide some automatic text processing. For example, the Get_Line ignores white spaces at the beginning of a line (Get_Immediate does not present this behavior), or adding a newline character when closing the file. This is thus adequate for applications handling simple textual data, but users requiring direct management of text (e.g. raw access to the character encoding) must consider other packages like Sequential_IO.

Direct I/OEdit

Direct I/O is used for random access files which contain only elements of one specific type. With Direct_IO you can position the file pointer to any element of that type (random access), however you can't freely choose the element type, the element type needs to be a definite subtype.

Sequential I/OEdit

Direct I/O is used for random access files which contain only elements of one specific type. With Sequential_IO it is the other way round: you can choose between definite and indefinite element types but you have to read and write the elements one after the other.

Stream I/OEdit

Stream I/O is the most powerful input/output package which Ada provides. Stream I/O allows you to mix objects from different element types in one sequential file. In order to read/write from/to a stream each type provides a 'Read and 'Write attribute as well as an 'Input and 'Output attribute. These attributes are automatically generated for each type you declare.

The 'Read and 'Write attributes treat the elements as raw data. They are suitable for low level input/output as well as interfacing with other programming languages.

The 'Input and 'Output attribute add additional control informations to the file, like for example the 'First and 'Last attributes from an array.

In object orientated programming you can also use the 'Class'Input and 'Class'Output attributes - they will store and recover the actual object type as well.

Stream I/O is also the most flexible input/output package. All I/O attributes can be replaced with user defined functions or procedures using representation clauses and you can provide your own Stream I/O types using flexible object oriented techniques.

See alsoEdit

WikibookEdit

Ada Reference ManualEdit

Ada 95 Quality and Style GuideEdit

Last modified on 21 March 2013, at 07:08