I/O: Examples
Here are examples of using the print
statement with formatting.
Formats can be repeated and grouped by using parentheses ()
. To repeat a format, add a preceding number. Example:
The write
statement works similarly to the print
statement, but it allows one to specify where to print. The general form of the write
statement is:
The unit
is a number; the format
is a format string. The format is the same as for the print
statement. The unit number is a handle that points to the destination. Standard output (unit number 6) and standard error (unit number 0) are always accessible and do not have to be opened by the program. If the output is to be written to a file, then the file has to be opened with the open
statement first (see below).
Examples: Writing to standard output. The standard output channel has the unit number 6. An asterisk (*
) can also be used.
Examples: Same as before, but writing to standard error. The error channel has the unit number 0.
Unit number 5 is reserved for reading from standard input. An asterisk (*
) can also be used. Example:
The next example demonstrates how to prompt for input to read data:
To avoid the carriage return of the write
statement, i.e. to have the prompt right after the string, add the advance='no'
specifier: