Input and Output
Input and Output: Key Ideas
Key Ideas
- Numeric:
- Skip white space (including newlines)
- If width /= 0, reads exactly width chars (or up to newline)
- Declaration (slightly modified) in Ada.Integer_Text_IO
procedure get(Item : out Integer; Width : in Natural);
Characters and strings:
- Reads exactly the right number of characters, but
- Excludes newlines in count (ie skips newlines)
- Example:
c: Character;
s: String(1 .. 10);
...
get(c); -- Reads exactly one character, excluding newline(s)
get(s); -- Reads exactly 10 characters, excluding newline(s)
procedure get_line(s, len)
function get_line return String
function get_line return Unbounded_String
Problems can occur if data file is created in different OS
Input Exceptions:
- Data_Error: Invalid value (eg for numeric IO non-digit or out
of range)
- Constraint_Error: Out of range
- End_Error: Attempt to read past end of file
Input of Strings and Unbounded Strings
Input of Fixed Length Strings
- Reads exactly the right number of characters, but
- Excludes newlines in count (ie skips newlines)
- Example:
c: Character;
s: String(1 .. 10);
...
get(c); -- Reads exactly one character, excluding newline(s)
get(s); -- Reads exactly 10 characters, excluding newline(s)
Notice that number of characters in string must be known before
call
Procedure Get_Line wtih Fixed Length Strings
- Procedure Get_Line allows reading an entire line, but only up to a maximum length
- Declaration:
procedure Get_Line(Item: String; Last: out Natural)
- Reads remainder of line, up to length of string
- Last contains index of last character assigned
- Last is the number of characters read, if the indices start at 1
- Example:
c: Character;
s: String(1 .. 80);
len: Natural;
...
while not end_of_file loop
get_line(s, len);
put_line(s(1 .. len)); -- Output a slice
end loop;
-- Sample Input:
--|Hello there!
--|Nice to see you!
-- Sample Output:
--|Hello there!
--|Nice to see you!
Additional rules apply if remaining input line same length as string
Function Get_Line wtih Dyanmically-Allocated, Fixed-Length Strings
- Function Get_Line reads an entire line,
- Declaration:
function Get_Line return String
- Reads remainder of line, and returns that string
- If don't know length of line, must have dynamically allocated string
- Dynamically allocate with a declare block or
- Dynamically allocate as a procedure call
- Example:
while not end_of_file loop
declare
t: String := get_line;
begin
put_line(t);
end; -- declare
end loop;
-- Sample Input:
--|Hello there!
--|Nice to see you!
-- Sample Output:
--|Hello there!
--|Nice to see you!
Function Get_Line Used as a Parameter
- A call to function Get_Line can be used as a parameter
- Example:
while not end_of_file loop
declare
put_line( get_line );
end loop;
-- Sample Input:
--|Hello there!
--|Nice to see you!
-- Sample Output:
--|Hello there!
--|Nice to see you!
Function Get_Line For Unbounded Strings
- Declaration:
function Get_Line return Unbounded_String
- Example:
with Ada.Strings.Unbounded, Ada.Text_IO.Unbounded_IO;
use Ada.Strings.Unbounded, Ada.Text_IO.Unbounded_IO;
...
u: Unbounded_String;
while not end_of_file loop
u := get_line;
put_line(u);
end loop;
-- Sample Input:
--|Hello there!
--|Nice to see you!
-- Sample Output:
--|Hello there!
--|Nice to see you!
I/O of Enumeration Types
Enumeration Types
- Enumeration literals can be read and printed
type Day is (Sun, Mon, Tue, Wed, Thu, Fri, Sat);
package Day_IO is new Ada.Text_IO.Enumeration_IO(Day);
d: Day;
begin
while not end_of_file loop
Day_IO.get(d);
Day_IO.put(d);
new_line;
end loop;
-- Can use image and img attributes
put_line(d'img);
put_line(d'image);
put_line(Day'Image(d));
-- Sample Input:
--|Mon wed FRI
--| Thu Tue
--
-- Sample Output:
--|MON
--|WED
--|FRI
--|THU
--|TUE
--|TUE
--|TUE
--|TUE
Get skips white space (blanks and newlines) until it finds a
valid literal for the type
A Data_Error exception occurs of a non-valid value is found
(eg Friday)
Input is case insensitive
Different options are available for case of the output
Character Enumerations
- Enumeration literals can be characters
- Unfortunately, the single quotes appear in the output and are
expected in the input
- The following example shows how the charater enumeration does
not really do what you want
type Vowel is ('a', 'e', 'i', 'u');
package Vowel_IO is new Ada.Text_IO.Enumeration_IO(Vowel);
v: Vowel;
begin
while not end_of_file loop
Vowel_IO.get(d);
Vowel_IO.put(d);
new_line;
end loop;
-- Sample Input:
--|'a' 'o'
-- Sample Output:
--|'a'
--|'o'
So, IO for type Character is special and is different from
the example
Other Points
White Space Summary
- Numeric and enum IO: Skips blanks and newlines until valid
literal is found
- Unless width parameter is non-zero
- Character and String IO: Blanks are not skipped, newlines
are skipped
Other Numeric IO Routines
- Get_Immediate: Read immediately, without buffering (ie without
interactively pressing return)
- Look_Ahead: Look at next character without advancing input
marker
Echo a file, including newlines
- A c idiom for echo-printing a file is (not compiled or tested!):
while(c=getc())
putc(c);
A similar program in Ada does not use Ada.Text_IO (since Text_IO does not read EOL)
To read the individual end-of-line characters as characters, you can use Ada.Sequential_IO.
This example opens and reads from the file "myFile.txt"; I don't know how to do the same from Standard Input.
with Ada.Sequential_IO;
with ada.text_io; -- use ada.text_io;
procedure putch3 is
package Char_IO is new Ada.Sequential_IO(Character);
-- use Char_IO;
MyFile: Char_IO.File_Type;
c: character;
begin
Char_IO.open (File => MyFile, Name => "myFile.txt", Mode => Char_IO.In_File);
while not Char_IO.end_of_file(MyFile) loop
Char_IO.read (MyFile, c);
Ada.Text_IO.put(c);
-- put_line(character'pos(c)'img); -- If you want the ASCII value of c
end loop;
Char_IO.Close (File => MyFile);
end putch3;
From the ITEC 320 FAQ