A.10.7 Input-Output of Characters and Strings
Static Semantics
For an item of type
Character the following procedures are provided:
procedure Get(File : in File_Type; Item : out Character);
procedure Get(Item : out Character);
After skipping any line terminators and any page
terminators, reads the next character from the specified input file and
returns the value of this character in the out parameter Item.
The exception End_Error
is propagated if an attempt is made to skip a file terminator.
procedure Put(File : in File_Type; Item : in Character);
procedure Put(Item : in Character);
If the line length
of the specified output file is bounded (that is, does not have the conventional
value zero), and the current column number exceeds it, has the effect
of calling New_Line with a spacing of one. Then, or otherwise, outputs
the given character to the file.
procedure Look_Ahead (File : in File_Type;
Item : out Character;
End_Of_Line : out Boolean);
procedure Look_Ahead (Item : out Character;
End_Of_Line : out Boolean);
Status_Error is
propagated if the file is not open. Mode_Error is propagated if the mode
of the file is not In_File. Sets End_Of_Line to True if at end of line,
including if at end of page or at end of file; in each of these cases
the value of Item is not specified.
Otherwise, End_Of_Line
is set to False and Item is set to the next character (without consuming
it) from the file.
procedure Get_Immediate(File : in File_Type;
Item : out Character);
procedure Get_Immediate(Item : out Character);
Reads the next character,
either control or graphic, from the specified File or the default input
file. Status_Error is propagated if the file is not open. Mode_Error
is propagated if the mode of the file is not In_File. End_Error is propagated
if at the end of the file. The current column, line and page numbers
for the file are not affected.
procedure Get_Immediate(File : in File_Type;
Item : out Character;
Available : out Boolean);
procedure Get_Immediate(Item : out Character;
Available : out Boolean);
If a character,
either control or graphic, is available from the specified File or the
default input file, then the character is read; Available is True and
Item contains the value of this character. If a character is not available,
then Available is False and the value of Item is not specified.
Status_Error
is propagated if the file is not open. Mode_Error is propagated if the
mode of the file is not In_File. End_Error is propagated if at the end
of the file. The current column, line and page numbers for the file are
not affected.
For an item of type
String the following subprograms are provided:
procedure Get(File : in File_Type; Item : out String);
procedure Get(Item : out String);
Determines the length
of the given string and attempts that number of Get operations for successive
characters of the string (in particular, no operation is performed if
the string is null).
procedure Put(File : in File_Type; Item : in String);
procedure Put(Item : in String);
Determines the length
of the given string and attempts that number of Put operations for successive
characters of the string (in particular, no operation is performed if
the string is null).
function Get_Line(File : in File_Type) return String;
function Get_Line return String;
Returns a result
string constructed by reading successive characters from the specified
input file, and assigning them to successive characters of the result
string. The result string has a lower bound of 1 and an upper bound of
the number of characters read. Reading stops when the end of the line
is met; Skip_Line is then (in effect) called with a spacing of 1.
Constraint_Error
is raised if the length of the line exceeds Positive'Last; in this case,
the line number and page number are unchanged, and the column number
is unspecified but no less than it was before the call.
The exception End_Error is propagated if an attempt is made to skip a
file terminator.
procedure Get_Line(File : in File_Type;
Item : out String;
Last : out Natural);
procedure Get_Line(Item : out String;
Last : out Natural);
Reads successive characters from the specified
input file and assigns them to successive characters of the specified
string. Reading stops if the end of the string is met. Reading also stops
if the end of the line is met before meeting the end of the string; in
this case Skip_Line is (in effect) called with a spacing of 1.
The
values of characters not assigned are not specified.
If characters are
read, returns in Last the index value such that Item(Last) is the last
character assigned (the index of the first character assigned is Item'First).
If no characters are read, returns in Last an index value that is one
less than Item'First. The exception End_Error is propagated if an attempt
is made to skip a file terminator.
procedure Put_Line(File : in File_Type; Item : in String);
procedure Put_Line(Item : in String);
Calls the procedure Put for the given string,
and then the procedure New_Line with a spacing of one.
Implementation Advice
The Get_Immediate procedures should be implemented
with unbuffered input. For a device such as a keyboard, input should
be “available” if a key has already been typed, whereas for
a disk file, input should always be available except at end of file.
For a file associated with a keyboard-like device, any line-editing features
of the underlying operating system should be disabled during the execution
of Get_Immediate.
31 Get_Immediate can be used to read a
single key from the keyboard “immediately”; that is, without
waiting for an end of line. In a call of Get_Immediate without the parameter
Available, the caller will wait until a character is available.
32 In a literal string parameter of Put,
the enclosing string bracket characters are not output. Each doubled
string bracket character in the enclosed string is output as a single
string bracket character, as a consequence of the rule for string literals
(see
2.6).
33 A string read by Get or written by Put
can extend over several lines. An implementation is allowed to assume
that certain external files do not contain page terminators, in which
case Get_Line and Skip_Line can return as soon as a line terminator is
read.
Ada 2005 and 2012 Editions sponsored in part by Ada-Europe