Ada Programming/Libraries/Ada.Text IO

From Wikibooks, open books for an open world
Jump to: navigation, search

Ada Lovelace 1838.jpg

The package Text_IO[edit]

Used for simple Input Output (IO) in text format.

Tips and Tricks for Text_IO[edit]

Read a whole line from the console[edit]

Ada 2005 has a function Get_Line which returns a newly created string containing the whole line:

function Get_Line return String;

With older Ada generations, you need a little work to get the complete line with one call. The Get_Line procedure gets a line of text of as many characters the Item can hold or up to the new_line indicator, whichever comes first. (The new_line indicator's representation is implementation defined.) It has the following specification:

procedure Get_Line (Item: out String; Last: out Natural);

To be specific, consider an Item that can hold as much as 80 characters. Let's take two lines to read, one holding less that 80 characters, say 10, the other at least 80, perhaps more. Calling

Get_Line (Item, Last);

will read the first line up to the new_line indicator and consume them; Item's first 10 characters will be filled with the text read, the rest is junk; Last will hold the last filled index.

The next call will read the maximum Item can hold, i.e. 80 characters; the rest of the line (if any) and the new_line indicator remain unconsumed. Thus Last will be Item'First - 1 + 80. In order to consume the rest of the line, you have to call Get_Line again. The result will be like one of the two possibilities above, depending on the remaining line length. If no characters are read (i.e. when the new_line indicator is the only thing left unread), Last will hold value Item'First - 1.

The following example shows how the complete line could be read:

with Ada.Text_IO;
with Ada.Strings.Unbounded;

function Get_Line return String is

  package Ustr renames Ada.Strings.Unbounded;
  package T_IO renames Ada.Text_IO;

  Everything: Ustr.Unbounded_String := Ustr.Null_Unbounded_String;
  Item      : String (1 .. 80);
  Last      : Natural;


    T_IO.Get_Line (Item, Last);                                        -- *
    Ustr.Append (Source => Everything, New_Item => Item (1 .. Last));  -- *
    exit Get_Whole_Line when Last < Item'Last;  -- **
  end loop Get_Whole_Line;

  return Ustr.To_String (Everything);

end Get_Line;

As an exercise, change the calls at (*) to

T_IO.Get_Line (Item (11 .. 20), Last);
Ustr.Append (Source => Everything, New_Item => Item (11 .. Last));

and see which values Item and Last will hold. Which criterium will you then need to exit the loop at (**)?

(This is of course not a very sensible idea to code like this, but as a learning instruction, it's fine.)

Read a whole line from a file[edit]

In principle it is the same as in console reading but you have to check for the end of file as well:

exit Get_Whole_Line when Last < Item'Last
                      or T_IO.End_Of_File (File);

End_of_File is always False for console input (except when you manage to enter the implementation defined end_of_file indicator). A well-formed text file (i.e. one created with Ada.Text_IO) will always hold an end_of_line (and an end_of_page) indicator before the end_of_file indicator (see procedure Close).

See also[edit]


Ada 95 Reference Manual[edit]

Ada 2005 Reference Manual[edit]

Ada Quality and Style Guide[edit]