Ada Programming/Input Output

From Wikibooks, the open-content textbooks collection

Jump to: navigation, search

Contents


Ada has 5 independent libraries for input/output operations. So the most important lesson to learn is choosing the right one.

[edit] Text I/O

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;

[edit] Direct I/O

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.

[edit] Sequential I/O

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.

[edit] Storage I/O

Storage I/O allows you to store one element inside a memory buffer. The element needs to be a definite subtype. Storage I/O is useful in Concurrent programming where it can be used to move elements from one task to another.

[edit] Stream I/O

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.

[edit] See also

[edit] Wikibook

[edit] Ada 95 Reference Manual

[edit] Ada 2005 Reference Manual

[edit] Ada Quality and Style Guide

Personal tools
In other languages