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 with Ada 95[edit]

The Get_Line () procedure gets a line of text up until the receiving string fills up. If the line is longer than the buffer then you need to read it more than once. The following example from Mathematical calculations shows how this is done:

File: numeric_4.adb (view, plain text, download page, browse all)
with Ada.Text_IO;
with Ada.Strings.Unbounded;
procedure Numeric_4 is
  package Str renames Ada.Strings.Unbounded;
  package T_IO renames Ada.Text_IO;


  function Get_Line return Str.Unbounded_String is
     BufferSize : constant := 2000;
     Retval     : Str.Unbounded_String := Str.Null_Unbounded_String;
     Item       : String (1 .. BufferSize);
     Last       : Natural;
     Get_Whole_Line :
           T_IO.Get_Line (Item => Item, Last => Last);

           Str.Append (Source => Retval, New_Item => Item (1 .. Last));

           exit Get_Whole_Line when Last < Item'Last;
        end loop Get_Whole_Line;
        return Retval;
  end Get_Line;


Ada 2005 has a function Get_Line which returns a newly created string which contains the whole string.

Reading a whole line from a file with Ada 95[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 IO.End_Of_File (File);

See also[edit]


Ada 95 Reference Manual[edit]

Ada 2005 Reference Manual[edit]

Ada Quality and Style Guide[edit]