HP OpenVMS Systems Documentation

Content starts here

HP Pascal for OpenVMS
Language Reference Manual

Previous Contents Index

9.5.4 Writing Partial Lines to Terminals

The WRITE procedure buffers output to the terminal until the WRITELN procedure is called. If too many characters are buffered, it can cause the HP Pascal buffer to overflow. The default size for this buffer is 255 characters for TEXT files. If you want to increase the internal buffer size, you can explicitly open the predeclared file OUTPUT with a larger record length. Consider the following example:


If you want each record to go directly to the terminal without buffering until the next WRITELN, you can explicitly open the predeclared file variable OUTPUT without carriage control. In this mode, the WRITELN procedure will write the information to the file without adding any carriage control. However, you need to include the carriage return and the line-feed characters in the output strings; the WRITELN procedure no longer provides these automatically. Consider the following example:

LF = 10;  {ASCII control characters}
CR = 13;
{In the executable section:}
WRITELN( ''(LF)'Output this' );
WRITELN( 'string directly' );
WRITELN( 'to the terminal'(CR) );

This is useful when you are writing escape sequences or other graphics characters to terminal devices.

9.6 Formatting Output

The output values of a WRITE, WRITELN, or WRITEV procedure can be compile-time or run-time expressions, with values of any ordinal, real, or string type. Each value is written with a default field width, which specifies the minimum number of characters to be written for the value. You can, however, override the default as described in in Section 9.6.1, Section 9.6.2, and Section 9.6.3. Table 9-6 lists the default field widths.

Table 9-6 Default Field Widths
Type of Item Printed Number of Characters
Enumerated Size of the longest identifier plus 1, up to 32
Character string Length of string

9.6.1 Specifying the Field Width

When you write any value, you can specify a field width to override the default. The format is identical for the WRITE, WRITELN, and WRITEV procedures. The formats are:

REAL format

output[[:minimum[[:fraction]] ]]

INTEGER format

output[[:minimum [[:radix]] ]]

String, Boolean, and enumeration format



The expression to be written, as you would write it without specifying the field width.


A nonnegative integer expression for the minimum number of characters to be written for the value. Pascal uses a greater field width if required by the magnitude of the number to be printed.


The fraction, which is permitted only for values of real types, indicates the number of digits to be written to the right of the decimal point.


The radix, which is permitted only for values of integer types, specifies the use of a base notation other than decimal.

If you try to write a number is too large to fit in the field that you specify, Pascal extends the field instead of removing digits and writing an incorrect number. If you specify a field width that is larger than necessary, Pascal prints blanks to the left of the number, right-justifying it.

If you try to write a value of an enumerated type, a Boolean value, a character, or a string value in a field that is too narrow, the value is truncated on the right. The truncated identifier is not checked for uniqueness.

9.6.2 Writing Real Numbers

By default, real numbers are written in exponential format. Regardless of the real number's type, output procedures always prefix the exponent with the letter E. Each real number in exponential format is preceded by a blank or a minus sign, and the value of the rightmost digit is rounded. Consider the following example:

WRITELN( Shoe_Size );

If the value of Shoe_Size is 12.5, this procedure produces the following output:


To write the value in decimal format, you must specify a field width as in this example:

WRITELN( Shoe_Size:5:1 );

The first integer indicates that a minimum of five characters will be written. The minimum includes the minus sign, if needed, and the decimal point. The second integer specifies one digit to the right of the decimal point. The resulting output is as follows:


If the field specified is wider than necessary, the value is written with leading blanks.

If you try to write a real value in a field that is too narrow, the field width is expanded to the minimum necessary to write the value.

9.6.3 Explicitly Specifying the Base

To write integers in a base other than decimal, supply the second run-time expression, which specifies the base, or radix.

integer-expression : fieldwidth : radix


Any run-time integer expression with a value from 2 through 36 inclusive. If you specify a base greater than 10, letters A through Z denote the extra digits. For example, when you output in hexadecimal (base 16), the characters A through F are the extra six digits.

If the integer-expression denotes a negative number, a leading minus sign is printed and the absolute value of the expression is converted into the selected radix.

9.6.4 Specifying the Base with Predeclared Conversion Functions

You can use the predeclared conversion functions BIN, DEC, UDEC, HEX, and OCT in combination with the WRITE, WRITELN, and WRITEV procedures to write binary, decimal, unsigned decimal, hexadecimal, and octal values. The DEC and UDEC functions return values with leading zeros; by default, the I/O routines use leading blank with decimal numbers, not leading zeros. The syntax is:

                                    {BIN   }
                                    {DEC   }
         WRITE( [[file_variable, ]] {UDEC  } ( expression[[, length[[,digits]] ]] ) ,...)
                                    {HEX   }
                                    {      }
                                    {OCT   }

The predeclared conversion functions convert the value of the first expression in the list to its equivalent as a binary, decimal, unsigned decimal, hexadecimal, or octal number. The resulting digits are returned in a VARYING OF CHAR string.

For every expression whose binary, decimal, unsigned decimal, hexadecimal, or octal value you wish to write, you must call the appropriate conversion function separately with an actual parameter list. You can call more than one conversion function in the same output procedure call. You can write variables of any type (including pointers) to text files in binary, decimal, unsigned decimal, hexadecimal, or octal notation.

You can specify field widths with the conversion functions; however, the results are not likely to be what you expect. For example, if you want to convert the value of i to its hexadecimal equivalent and you want the converted value to be written in a field three characters wide, you might write the following procedure call:

WRITELN( HEX( i ):3 );

However, because the converted value is longer than the field width specification, the value is truncated on the right rather than on the left. Therefore, the output generated by this procedure would be as follows:


Be careful about specifying field widths with BIN, DEC, UDEC, HEX, and OCT when the converted value could exceed the field width given.

Consider the following example:

WRITE( HEX( Payroll, 10 ), HEX( Salary, 12 ) );

The values of the variables Payroll and Salary are converted to their hexadecimal equivalents. Payroll is printed with 10 characters and Salary is printed with 12 characters. The output values, preceded by two initial blanks, could look like this:

000031F2    000058AB

Consider the following example:

WRITELN( OCT( Social_Security, 14 ), BIN( Survey, 8 ) );

The value of the variable Social_Security is converted to its octal equivalent and printed with 14 characters. The value of the variable Survey is then converted to its binary equivalent and printed with eight characters. A sample line of output, preceded by three blanks, could look like this:


Consider the following example:

WRITEV( Final_Balance, OCT( Debits, 16 ), OCT( Credits, 16 ) );

The values of the variables Debits and Credits are converted to their octal equivalents and written to the string variable Final_Balance with 16 characters each. The output string, preceded by five blanks, could look like this:

'     77777770342     00000033766'

9.6.5 Writing Nonnumeric Types

For an expression of an enumerated type, the constant identifier denoting the expression's value is written. Consider the following example:

Color : ( Blue, Yellow, Black, Fire_Engine_Green );
{In the executable section:}
WRITE( 'My favorite color is ', Color:15 );

When the value of Color is Yellow, the following is written:

My favorite color is          YELLOW

When the value of Color is Fire_Engine_Green, the following appears:

My favorite color is FIRE_ENGINE_GRE

Because the field width specified in these cases is not wide enough for all 17 characters in the identifier, the identifier is truncated after the field is filled.

For More Information:

9.7 Error-Processing Parameter

For I/O procedures, the last parameter (which is optional) specifies the action to be taken should the procedure fail to execute successfully. You must use nonpositional syntax in order to pass the error-recovery parameter to the called procedure. This parameter is called ERROR and can accept two values: CONTINUE and MESSAGE.

If you specify ERROR := CONTINUE, the program continues to execute regardless of any error conditions encountered during execution of the procedure. If you specify this value, you should use the STATUS function to be certain that the I/O routine worked as expected.

If you specify ERROR := MESSAGE and if an error occurs, HP Pascal generates an appropriate error message and program execution stops. By default, HP Pascal displays an error message and program execution stops after the first error in an I/O operation.

You cannot use the error-recovery parameter with the I/O functions EOF, UFB, and EOLN, nor with any reference to the file buffer.

9.8 I/O Routines

HP Pascal provides predeclared procedures and functions to perform input and output operations on file variables. These routines may operate differently depending on a file's organization and the currently defined access method.

The I/O routines in the following sections appear in alphabetical order. To save you the time of scrolling through all the routines, you may wish to click on the Table Of Contents entry for the desired routine.

At any time during the execution of a process, a file variable is considered to be in one of three modes: inspection, generation, or undefined. When a file is reading input, it is in inspection mode. When output is being written to a file, the file is in generation mode. A file in an undefined state of processing is in undefined mode. The mode often determines the valid operations for the file.

Table 9-7 shows the mode required before execution of each I/O routine and shows the mode in which the file is left after each routine has executed.

Table 9-7 File Mode During I/O Processing
I/O Routine Mode Before
Mode After
I/O Routine Mode Before
Mode After
CLOSE Any Undefined READ Inspection Inspection
DELETE Inspection Inspection READLN Inspection Inspection
EOF Inspection or
No change RESET Any Inspection
EOLN Inspection Inspection RESETK Any Inspection
EXTEND Any Generation REWRITE Any Generation
FIND Any Inspection if
undefined if
STATUS Any No change, unless error
FINDK Any Inspection
if successful;
if unsuccessful
TRUNCATE Inspection Generation
GET Inspection Inspection UFB Any No change
LINELIMIT Any No change UNLOCK Inspection Inspection
LOCATE Any Generation UPDATE Inspection Inspection
OPEN Undefined Undefined WRITE Generation,
unless keyed
access, which
may be any
PAGE Generation No change WRITELN Generation Generation
PUT Generation Generation      

9.8.1 CLOSE Procedure

The CLOSE procedure closes an open file. You can use either positional or nonpositional syntax in the call.

    1. CLOSE (file_variable
              ,[[ERROR := error_recovery]] )

    2. CLOSE ( FILE_VARIABLE := file_variable
              [[,DISPOSITION := disposition]]
              [[,USER_ACTION := user_action]]
              [[,ERROR := error_recovery   ]] ...)


no default

The name of the file variable associated with the file that HP Pascal is to close.


same as for OPEN procedure

A value that determines what HP Pascal is to do with the file after closing it. The disposition values are the same as those used for the OPEN procedure. The disposition value in the CLOSE procedure supersedes a disposition value specified in the OPEN procedure.


no default

A routine name that HP Pascal calls to close the file. You can use a user-action routine to close the file using environment-specific capabilities.


stops execution after first error (default)

The action to be taken if an error occurs during execution of the routine.

Execution of the CLOSE procedure causes the system to close the file and, if the file is internal, to delete it. Each file is automatically closed when control passes from the block in which it is declared.

You cannot close a file that has not been opened (either explicitly by the OPEN procedure, or implicitly by the EXTEND, RESET, or REWRITE procedure). If you try to close a file that was never opened, an error occurs.

The file can be in any mode (inspection, generation, or undefined) before the CLOSE procedure is called. Execution of CLOSE sets the mode to undefined.

For More Information:

Previous Next Contents Index