The Text_IO package handles reading and writing to the console (or UNIX
tty device) and text files. The package name "text_io" should be omitted
when invoking these subroutines.
The predefined enumerated type file_mode determines if a file will be
read (in_file), written to (out_file) or appended to (append_file). If a file is appended to, it should already exist.
There are several predefined file_type constants: standard_input,
standard_output and standard_error can be used to redirect I/O
to your operating system's standard input, output or error respectively.
There are also three file_type aliases. These can be used
anywhere a file_type variable is expected.
current_input -- an alias for the current input file
current_output-- an alias for the current output file
current_error -- an alias for the current error file
The current aliases can be redirected using set_output or related
procedures.
=> set_output( f )
From now on, all output going to standard output is written to the file_type
variable f. The current_output function refers to the file being written
to (in this case, f). Output redirected to f is not counted by the line
function.
The put, put_line, get, get_line and new_line commands are used to read
and write files.
=> f : file_type
=> create( f, out_file, "data.out" )
=> put_line( f, "Added to file" )
=> ? name( f )
data.out
=> ? mode( f )
out_file
=> ? line( f )
1
=> close( f )
Example: Writing to a text file
? e (question mark command)
Display expression e to current output. If appropriate, removes the decimal part of the expression (i.e. 41.0 will be displayed as 41) so as more human-friendly. If f is specified, writes to the file f instead of current_output. The expression type can be a string type, a numeric type or an enumerated type. Limited types are not allowed.
Read the first character from a line on standard input and return it as c. Don't display the character. Return immediately if there's no data when b is true.
This function is incomplete. It was introduced in SparForte 1.5 Displaying the character is turned off when get_immediate is executed so if a user types a character before get_immediate is executed, it will still appear.
s := get_line [( f )]
Read a string until the end of line is encountered. If f is specified, read from file f instead of current_input. End-of-line characters are not returned. To read a numeric value, convert the string value to a numeric value with numerics.value and typecast it if necessary.
Read a character from current_input. It does not write the character back to current_output.
Example
ch := inkey;
Parameters
Param
Mode
Type
Default
Description
c
return value
character
required
the key that was pressed
Exceptions
-
See Also
-
Compare With
BASIC: inkey$
Rationale
get is provided for compatibility for Ada 95 but get is not very convenient. inkey is a more useful function for reading a keypress.
is_open( f )
Return true if file f is open
Example
if is_open( data ) then ...
Parameters
Param
Mode
Type
Default
Description
f
in
file_type
required
file variable representing the file to test
Exceptions
-
See Also
-
Compare With
Ada: Ada.Text_IO.Is_Open
i := line( f )
Return the number of lines read or written to file f (with put_line, the number of lines explicitly put to that file). This function will work with standard_input, standard_output and standard_error. However, it does not take into account lines read or written by operating system commands.
Example
if line( f ) > 33 then ...
Parameters
Param
Mode
Type
Default
Description
i
return value
integer
required
the number of lines read or written
f
in
file_type
required
file variable representing the file to test
Exceptions
An exception will be raised if the file is not open
Return the pathname of the open file. This function will work with standard_input, standard_output and standard_error. However, it does not return a pathname since these files have no known pathnames.
Example
put_line( "error in file " & name( f ) );
Parameters
Param
Mode
Type
Default
Description
s
return value
string
required
the pathname of the file
f
in
file_type
required
file variable representing the file
Exceptions
An exception will be raised if the file is not open
Write expression e with optional numeric formatting. The
expression type can be a string type, a numeric type or an enumerated type.
Limited types are not allowed. If f is specified, writes to the file f instead
of current_output. If picture string p is used, formats a numeric value
according to the picture as described in
Numeric Formatting with Put in the
reference section.
Write expression e with an appended line feed (new line) character. The
expression type can be a string type, a numeric type or an enumerated type.
Limited types are not allowed. If f is specified, writes to the file f instead
of current_output.
Redirect the current error file (usually standard_error) to another open file f. All future writes to current_error (put_line( current_error, ...), etc.) will use the new file. The new file must be an out_mode file. Use set_error( standard_error ) to return to standard error, or save the value of current_error and restore it later.
Example
set_error( log_file );
Parameters
Param
Mode
Type
Default
Description
f
in
file_type
required
the file to write error output to
Exceptions
If the file is not open or if it is the wrong mode, an exception will be raised
Redirect the current input (usually standard_input) to another open file f. All future reads (get, get_line, etc.) will use the new file. The new file must be an in_file mode file. Use set_input( standard_input ) to return to standard input, or save the value of current_input and restore it later.
Example
set_input( data_file );
Parameters
Param
Mode
Type
Default
Description
f
in
file_type
required
the file to read input from
Exceptions
If the file is not open or if it is the wrong mode, an exception will be raised
Redirect the current output (usually standard_output) to another open file f. All future writes (put, put_line, etc.) will use the new file. The new file must be an out_mode file. Use set_output( standard_output ) to return to standard output, or save the value of current_output and restore it later.
Example
set_output( data_file );
Parameters
Param
Mode
Type
Default
Description
f
in
file_type
required
the file to write output to
Exceptions
If the file is not open or if it is the wrong mode, an exception will be raised