Ada Programming/Libraries/Ada.Text IO

is a unit of the Predefined Language Environment since Ada 95.

Description
The package Text_IO is used for simple Input Output (I/O) in text format.

Read a whole line from the console
Ada 2005 has a function  which returns a newly created string containing the whole line:

Get_Line String;

With older Ada generations, you need a little work to get the complete line with one call. The  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:

Get_Line (Item: String; Last:  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. In order to consume the rest of the line, you have to call  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),   will hold value.

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

Ada.Text_IO; Ada.Strings.Unbounded; Get_Line String Ustr Ada.Strings.Unbounded; T_IO Ada.Text_IO; Everything: Ustr.Unbounded_String := Ustr.Null_Unbounded_String; Item     : String (1 .. 80); Last     : Natural; Get_Whole_Line: T_IO.Get_Line (Item, Last);                                       -- * Ustr.Append (Source => Everything, New_Item => Item (1 .. Last)); -- * Get_Whole_Line Last < Item';  -- ** Get_Whole_Line; Ustr.To_String (Everything); 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  and   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
In principle it is the same as in console reading but you have to check for the end of file as well:

Get_Whole_Line Last < Item' 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 ) will always hold an end_of_line (and an end_of_page) indicator before the end_of_file indicator (see procedure ).

Generic nested packages
has the following nested packages for input/output of scalar types. The only parameter is the involved type.
 * Decimal_IO
 * Enumeration_IO
 * Fixed_IO
 * Float_IO
 * Integer_IO
 * Modular_IO

Specification
File_Type File_Mode In_File Out_File Append_File Count  0  implementation_defined Positive_Count Count  1  Count Unbounded  Count  0 Field       Integer  0  implementation_defined Number_Base Integer  2  16 Type_Set Lower_Case Upper_Case Create File   File_Type Mode  File_Mode  Out_File Name  String     "" Form  String     "" Open  File    File_Type Mode  File_Mode Name  String Form  String  "" Close File    File_Type Delete File   File_Type Reset File    File_Type Mode   File_Mode Reset File    File_Type Mode  File   File_Type  File_Mode Name  File   File_Type  String Form  File   File_Type  String Is_OpenFile  File_Type  Boolean Set_Input File  File_Type Set_OutputFile  File_Type Set_Error File  File_Type Standard_Input  File_Type Standard_Output File_Type Standard_Error  File_Type Current_Input   File_Type Current_Output  File_Type Current_Error   File_Type File_Access   File_Type Standard_Input  File_Access Standard_Output File_Access Standard_Error  File_Access Current_Input   File_Access Current_Output  File_Access Current_Error   File_Access Flush File   File_Type Flush Set_Line_Length File  File_Type To   Count Set_Line_Length To    Count Set_Page_Length File  File_Type To   Count Set_Page_Length To    Count Line_Length File  File_Type  Count Line_Length Count Page_Length File  File_Type  Count Page_Length Count New_Line  File      File_Type Spacing  Positive_Count  1 New_Line  Spacing   Positive_Count  1 Skip_Line File      File_Type Spacing  Positive_Count  1 Skip_Line Spacing   Positive_Count  1 End_Of_Line File  File_Type  Boolean End_Of_Line Boolean New_Page  File   File_Type New_Page Skip_Page File   File_Type Skip_Page End_Of_Page File  File_Type  Boolean End_Of_Page Boolean End_Of_File File  File_Type  Boolean End_Of_File Boolean Set_Col File  File_Type To   Positive_Count Set_Col To    Positive_Count Set_Line File  File_Type To   Positive_Count Set_Line To    Positive_Count Col File  File_Type  Positive_Count Col  Positive_Count Line File  File_Type  Positive_Count Line Positive_Count Page File  File_Type  Positive_Count Page Positive_Count Get File   File_Type Item   Character Get Item  Character Put File   File_Type Item   Character Put Item   Character Look_Ahead File          File_Type Item         Character End_Of_Line  Boolean Look_Ahead Item         Character End_Of_Line  Boolean Get_Immediate File        File_Type Item       Character Get_Immediate Item       Character Get_Immediate File        File_Type Item       Character Available  Boolean Get_Immediate Item       Character Available  Boolean Get File   File_Type Item   String Get Item  String Put File   File_Type Item   String Put Item   String Get_Line File   File_Type Item  String Last  Natural Get_Line Item  String Last   Natural Get_LineFile   File_Type  String Get_Line String Put_Line File   File_Type Item   String Put_Line Item   String Num Integer_IO Default_Width Field  Num Default_Base  Number_Base  10 Get File    File_Type Item   Num Width  Field  0 Get Item   Num Width   Field  0 Put File   File_Type Item   Num Width  Field  Default_Width Base   Number_Base  Default_Base Put Item   Num Width  Field  Default_Width Base   Number_Base  Default_Base Get From   String Item  Num Last  Positive Put To    String Item  Num Base  Number_Base  Default_Base Integer_IO Num Modular_IO Default_Width Field  Num Default_Base  Number_Base  10 Get File    File_Type Item   Num Width  Field  0 Get Item   Num Width   Field  0 Put File   File_Type Item   Num Width  Field  Default_Width Base   Number_Base  Default_Base Put Item   Num Width  Field  Default_Width Base   Number_Base  Default_Base Get From   String Item  Num Last  Positive Put To    String Item  Num Base  Number_Base  Default_Base Modular_IO Num Float_IO Default_Fore Field  2 Default_Aft  Field  Num1 Default_Exp  Field  3 Get File    File_Type Item   Num Width   Field  0 Get Item   Num Width   Field  0 Put File  File_Type Item  Num Fore  Field  Default_Fore Aft   Field  Default_Aft Exp   Field  Default_Exp Put Item  Num Fore  Field  Default_Fore Aft   Field  Default_Aft Exp   Field  Default_Exp Get From  String Item  Num Last  Positive Put To    String Item  Num Aft   Field  Default_Aft Exp   Field  Default_Exp Float_IO Num Fixed_IO Default_Fore Field  Num Default_Aft  Field  Num Default_Exp  Field  0 Get File    File_Type Item   Num Width   Field  0 Get Item   Num Width   Field  0 Put File  File_Type Item  Num Fore  Field  Default_Fore Aft   Field  Default_Aft Exp   Field  Default_Exp Put Item  Num Fore  Field  Default_Fore Aft   Field  Default_Aft Exp   Field  Default_Exp Get From   String Item  Num Last  Positive Put To    String Item  Num Aft   Field  Default_Aft Exp   Field  Default_Exp Fixed_IO Num Decimal_IO Default_Fore Field  Num Default_Aft  Field  Num Default_Exp  Field  0 Get File    File_Type Item   Num Width   Field  0 Get Item   Num Width   Field  0 Put File  File_Type Item  Num Fore  Field  Default_Fore Aft   Field  Default_Aft Exp   Field  Default_Exp Put Item  Num Fore  Field  Default_Fore Aft   Field  Default_Aft Exp   Field  Default_Exp Get From   String Item  Num Last  Positive Put To    String Item  Num Aft   Field  Default_Aft Exp   Field  Default_Exp Decimal_IO Enum Enumeration_IO Default_Width   Field  0 Default_Setting Type_Set  Upper_Case Get File   File_Type Item  Enum Get Item  Enum Put File   File_Type Item   Enum Width  Field     Default_Width Set    Type_Set  Default_Setting Put Item   Enum Width  Field     Default_Width Set    Type_Set  Default_Setting Get From   String Item  Enum Last  Positive Put To    String Item   Enum Set    Type_Set  Default_Setting Enumeration_IO Status_Error   IO_ExceptionsStatus_Error Mode_Error     IO_ExceptionsMode_Error Name_Error     IO_ExceptionsName_Error Use_Error      IO_ExceptionsUse_Error Device_Error   IO_ExceptionsDevice_Error End_Error      IO_ExceptionsEnd_Error Data_Error     IO_ExceptionsData_Error Layout_Error   IO_ExceptionsLayout_Error File_Type AdaText_IO

Wikibook

 * Ada Programming
 * Ada Programming/Input Output
 * Ada Programming/Libraries
 * Ada Programming/Libraries/Ada
 * Ada Programming/Libraries/Ada.Strings.Unbounded.Text_IO

Open-Source Implementations
Programación en Ada/Unidades predefinidas/Ada.Text IO