Ada.Text_IO in Depth

Master console I/O: Put, Put_Line, New_Line, Get, Get_Line (procedure and function forms), End_Of_File loops, the generic numeric I/O packages (Integer_IO, Float_IO, Fixed_IO, Decimal_IO, Modular_IO, Enumeration_IO) with Width/Base/Fore/Aft/Exp formatting, columns with Set_Col, Standard_Error, and Get from strings.

Ada.Text_IO handles character-oriented input and output. Strings are written directly; numbers and enumerations are handled by generic child packages you instantiate for your own types (Integer_Text_IO and Float_Text_IO are predefined instances). Formatting is done with named parameters rather than format strings, which is verbose but type safe.

Output

Put (String or Character) writes text, Put_Line adds a line terminator, New_Line (N) ends lines. For numbers, instantiate Integer_IO (T), Float_IO (T), Fixed_IO, Decimal_IO, Modular_IO or Enumeration_IO (T) — or use Ada.Integer_Text_IO / Ada.Float_Text_IO. Integer Put takes Width (minimum field width, right aligned; 0 = as narrow as possible) and Base (2 .. 16, printing based literals like 16#FF#). Float Put takes Fore (digits before the point), Aft (after) and Exp (exponent digits; 0 = no exponent). Enumeration Put takes Set => Lower_Case | Upper_Case. Set_Col (N) moves to column N for simple tables.

• Put / Put_Line / New_Line / Set_Col. • Integer: Width, Base. Float: Fore, Aft, Exp. Enumeration: Width, Set. • Instantiate Integer_IO/Float_IO/Enumeration_IO/Decimal_IO for your types.

Input

Get_Line (Buffer, Last) reads a line into a fixed buffer and sets Last to the last filled index; the function form 'Line : constant String := Get_Line;' (Ada 2005) returns exactly the line without a buffer — the modern idiom. Numeric Get skips blanks and line terminators, then parses a literal, raising Data_Error on malformed input. End_Of_File tests for the end of input; End_Of_Line for the end of the current line. Skip_Line discards the rest of a line — needed after numeric Get if you then want Get_Line. Numeric Get from a String (Get (S, V, Last)) parses without I/O.

• Get_Line (Buf, Last) or Line : constant String := Get_Line; • Numeric Get skips whitespace; bad input → Ada.IO_Exceptions.Data_Error. • End_Of_File / End_Of_Line / Skip_Line. • Get (From_String, Item, Last) parses strings.

Standard Files

Standard_Input, Standard_Output and Standard_Error are File_Type objects; most operations have an overload taking a File as first parameter: Put_Line (Standard_Error, "message"). Set_Output / Set_Input redirect the defaults to other files. Errors belong on Standard_Error so that they are visible even when normal output is redirected into a file or pipe.

• Put_Line (Standard_Error, ...) for diagnostics. • Set_Output / Set_Input change the default files.

Code Examples

The program was run with this standard input: a line 'Lovelace', then '10 20' and '30'. Integer Put's default width is 11 columns (enough for Integer'First). Standard output and standard error were captured separately, and the error line is shown last. Output: 42 42 16#FF# 3.14 1.235E+3 GREEN blue 1234.50 Name Score Ada 98 Hello, Lovelace! Read 3 numbers, total = 60 Parsed 12 (last char used at 4) (this line was written to standard error)

Get (N); Get_Line (Name, Last); -- returns an empty line Get (N); Skip_Line; Get_Line (Name, Last);

Numeric Get leaves the line terminator; skip it before reading the next line.

Put_Line ("Total: " & Float'Image (X)); -- 1.23450E+02 Put ("Total: "); Put (X, Fore => 1, Aft => 2, Exp => 0); New_Line;

'Image uses scientific notation; Float_IO gives controlled formatting.

Buffer: String (1.. 80); Get_Line (Buffer, Last); -- longer lines are split Line: constant String:= Get_Line;

The function form reads the whole line regardless of length.

Which parameter controls digits after the decimal point in Float Put? A. Fore B. Aft C. Exp D. Width

What does Put (255, Width => 0, Base => 16) print? A. FF B. 0xFF C. 16#FF# D. 255 C. 16#FF# (verified).

Which exception does numeric Get raise on malformed input? A. Constraint_Error B. Data_Error C. Name_Error D. End_Error

B. Data_Error (End_Error if input is exhausted).

How do you write to standard error? A. Put_Line (Standard_Error, S) B. Put_Error (S) C. Put_Line (S, 2) D. Err.Put_Line (S)

Write a program that reads integers until end of file and prints their maximum.

with Ada.Text_IO; use Ada.Text_IO; with Ada.Integer_Text_IO; use Ada.Integer_Text_IO; procedure Max_Of_Input is N, Max : Integer := Integer'First; begin while not End_Of_File loop Get (N); Max := Integer'Max (Max, N); end loop; Put (Max, Width => 0); New_Line; end Max_Of_Input;

End_Of_File guards the loop; Get skips whitespace and newlines. (Trailing blank lines at the end of input would make Get raise End_Error; use Skip_Line checks for robust parsing.)

with Ada.Text_IO;         use Ada.Text_IO;
with Ada.Integer_Text_IO; use Ada.Integer_Text_IO;   --  predefined instance of Integer_IO
with Ada.Float_Text_IO;   use Ada.Float_Text_IO;

procedure L45_Text_IO is
   type Color is (Red, Green, Blue);
   package Color_IO is new Enumeration_IO (Color);    --  generic I/O for enumerations
   type Money is delta 0.01 digits 9;
   package Money_IO is new Decimal_IO (Money);

   Name  : String (1 .. 40);
   Last  : Natural;
   Count : Integer;
   Total : Integer := 0;
   Line_No : Natural := 0;
begin
   --  Formatted numeric output
   Put (42);                        New_Line;       --  default width
   Put (42, Width => 6);            New_Line;       --  right-aligned in 6 columns
   Put (255, Width => 0, Base => 16); New_Line;     --  based literal output
   Put (3.14159, Fore => 3, Aft => 2, Exp => 0);  New_Line;
   Put (1234.5, Fore => 1, Aft => 3, Exp => 2);   New_Line;
   Color_IO.Put (Green);  Put ("  ");  Color_IO.Put (Blue, Set => Lower_Case);  New_Line;
   Money_IO.Put (Money'(1234.5), Fore => 6);  New_Line;

   --  Columns
   Put ("Name");  Set_Col (12);  Put_Line ("Score");
   Put ("Ada");   Set_Col (12);  Put (98, Width => 5);  N
…