[an error occurred while processing this directive]

HP OpenVMS Systems Documentation

Content starts here

HP Pascal for OpenVMS
Language Reference Manual


Previous Contents Index

9.8.11 OPEN Procedure

The OPEN procedure opens a file and allows you to specify file characteristics using either positional or nonpositional syntax.


   1. OPEN( file_variable
             ,[[file_name]]
             ,[[history]]
             ,[[record_length]]
             ,[[access_method]]
             ,[[record_type]]
             ,[[carriage_control]]
             ,[[organization]]
             ,[[disposition]]
             ,[[file_sharing]]
             ,[[user_action]]
             ,[[default_file_name]]
             ,[[ERROR := error_recovery]] )

   2. OPEN( FILE_VARIABLE := file_variable
             [[,FILE_NAME := file_name]]
             [[,HISTORY := history]]
             [[,RECORD_LENGTH := record_length]]
             [[,ACCESS_METHOD := access_method]]
             [[,RECORD_TYPE := record_type]]
             [[,CARRIAGE_CONTROL := carriage_control]]
             [[,ORGANIZATION := organization]]
             [[,DISPOSITION := disposition]]
             [[,SHARING := file_sharing]]
             [[,USER_ACTION := user_action]]
             [[,DEFAULT := default_file_name]]
             [[,ERROR := error_recovery]] ... )

file_variable

no default

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

file_name

environment specific (default)

A character-string expression containing the external file name. HP Pascal determines the default file name according to the environment in which you are programming.

history

NEW (default for OPEN/REWRITE openings)

OLD (default for EXTEND/RESET openings)

A value that indicates whether the file exists or if HP Pascal must create the file. If you specify OLD and if HP Pascal cannot find the file, an error occurs.

Other values are READONLY and UNKNOWN. If you specify READONLY, you can only read from the file; if you attempt to write to the file, an error occurs. If you specify UNKNOWN, HP Pascal looks for an existing file but creates a new file if an existing file does not exist. If you specify OLD or UNKNOWN and if the attempt to open the file generates a file protection error, HP Pascal tries again using READONLY.

record_length

255 bytes (default for TEXT and FILE OF VARYING)

ignored (default for other file types)

A positive integer that specifies the maximum size in bytes for a line in a TEXT file or a file of type FILE OF VARYING. (Record length is equivalent to component length.) The default is 255 bytes. For all other types of files, HP Pascal ignores this parameter.

If you do not specify a length for an existing file, HP Pascal uses the length specified at the file's creation.

If you use OPEN to create a sequentially organized file with variable-length components, HP Pascal records the maximum length of each component in the file only if you specify a value for the record_type field.

access_method

SEQUENTIAL (default)

A value that specifies the component access method to use. The possible values include SEQUENTIAL, DIRECT, and KEYED. The DIRECT access method is equivalent to random access by relative component number. The KEYED access method is equivalent to random access by key.

record_type

VARIABLE (default for new TEXT and VARYING OF CHAR

FIXED (default for other new files)

A value that indicates the component format. (Record format and component format are equivalent.) The available values are FIXED (fixed-length components), VARIABLE (variable-length components), STREAM (stream component format with either carriage return, combination carriage return and line feed, or form-feed delimiters), STREAM_CR (stream component format with carriage-return delimiters), and STREAM_LF (stream component format with line-feed delimiters).

carriage_control

LIST (default for TEXT and VARYING OF CHAR files)

NONE (default for all other file types)

A value that indicates the carriage-control format for the file. The value LIST indicates single spacing between components. The values CARRIAGE and FORTRAN are equivalent and indicate that the first character of every output line is a carriage-control character. The values NONE and NOCARRIAGE indicate that the file has no carriage control.

organization

SEQUENTIAL (default for new files)

A value that specifies the file organization. If you are accessing an existing file, the specified organization must match the organization of the existing file; if it does not, an error occurs. The choices for this parameter are SEQUENTIAL, RELATIVE, and INDEXED.

disposition

SAVE (default for external files)

DELETE (default for internal files)

A value that indicates what HP Pascal should do with the file after you close the file. The dispositions are as follows:
Disposition Description
SAVE HP Pascal retains the file.
DELETE HP Pascal deletes the file.
PRINT HP Pascal prints the file on a line printer and retains the file.
PRINT_DELETE HP Pascal prints the file on a line printer and then deletes the file.
SUBMIT HP Pascal submits to a queue or places the print job in a background process and retains the file.
SUBMIT_DELETE HP Pascal submits to a queue or places the print job in a background process and deletes the file.

file_sharing

READONLY (default for HISTORY := READONLY)

NONE (default for other histories)

A value that specifies whether another program can access the file while it is open. A value of READONLY indicates that other programs can read but not write to the file. A value of READWRITE indicates that a program can both read and write to the file while it is open. A value of NONE indicates that a program cannot read or write from the open file.

default_file_name

no default

A string expression containing default file specification information. For instance, you can use this value to set a default directory specification.

user_action

no default

A name of a user-written routine that HP Pascal calls to open the file (instead of allowing HP Pascal to open the file with the OPEN procedure). You can use a user-action routine to open the file using environment-specific capabilities of the I/O system underlying HP Pascal.

error_recovery

stops execution after first error (default)

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

Using the OPEN procedure:

Before the OPEN procedure is called, the file is in undefined mode; its mode does not change after OPEN has been executed.

You cannot use OPEN on a file variable that is already open.

If you use INPUT or OUTPUT, HP Pascal implicitly opens them just before their first use. HP Pascal implicitly opens INPUT with a history of READONLY. If you choose, you can explicitly open INPUT or OUTPUT. To do this, call the OPEN procedure at any point in your compilation unit before you use the first I/O routine on that file.

Because the RESET, REWRITE, and EXTEND procedures implicitly open files, you need not always use the OPEN procedure. RESET, REWRITE, and EXTEND impose the same defaults as OPEN, except where noted (in the HISTORY parameter).

You must use the OPEN procedure to do the following:

  • Create a TEXT file with fixed-length components
  • Create a file with relative or indexed organization
  • Open a file for direct or keyed access
  • Specify a line length other than the default for a line in a TEXT file

Consider the following example:



PROGRAM Main( User_Guide );
VAR
User_Guide : TEXT;
{In the executable section:}
OPEN( User_Guide );

When the OPEN procedure is executed, the system first attempts to find an environment-specific translation for User_Guide. If no such translation happens, the file USER_GUIDE.DAT is created in the default device and directory on the local computer.

If User_Guide had not been specified as an external file in the program header, the OPEN procedure would have created an internal file. By default, the file is created with a record length of 255 bytes and components of variable length. The system then opens the file for sequential access.

Consider the following example:



OPEN( Journal_Accounts,
'JOURNAL.DAT',
HISTORY := UNKNOWN,
ACCESS_METHOD := KEYED,
ORGANIZATION := INDEXED );

If the file JOURNAL.DAT already exists, this procedure opens it; otherwise, HP Pascal creates a new file named JOURNAL.DAT with the specified characteristics. If the file does exist, it must have the same characteristics as those in the parameter list of the OPEN procedure. HP Pascal opens the file with indexed organization for keyed access.

For More Information:

9.8.12 PAGE Procedure

The PAGE procedure skips from the current page to the next page of a TEXT file.


PAGE( file_variable [[, ERROR := error-recovery]] );

file_variable

The name of the file variable associated with a TEXT file.

error-recovery

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

The file must be in generation mode before the PAGE procedure is called; the mode does not change as a result of the procedure's execution.

Execution of the PAGE procedure clears the record buffer, if it contains data, by performing a WRITELN procedure, and then advances the output to a new page of the specified TEXT file. The next component written to the file begins on the first line of a new page. You can use this procedure only on TEXT files. If you specify a file of any other type, an error occurs.

The value of the page eject component that is output to the file depends on the carriage-control format for that file. When CARRIAGE or FORTRAN is enabled, the page eject record is equivalent to the carriage-control character '1'. When LIST, NOCARRIAGE, or NONE is enabled, the page eject record is a single form feed character.

Consider the following example:


PAGE( User_Guide );

This PAGE procedure causes a page eject record to be written in the text file User_Guide.

For More Information:

9.8.13 PUT Procedure

The PUT procedure adds a new component to a file.


PUT( file_variable [[, ERROR := error-recovery]]

file_variable

The name of the file variable associated with the output file.

error-recovery

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

Before executing the first PUT procedure on a file opened for sequential access, you must execute an EXTEND, REWRITE, or TRUNCATE REWRITE, and TRUNCATE preparing the file for output. ( (TRUNCATE is legal only on files with sequential organization.)) If the file has indexed organization, the components to be written must be ordered by the primary key.

Before executing the first PUT statement on a file opened for direct access, you must execute an EXTEND, REWRITE, or LOCATE procedure to position the file.

The PUT procedure writes the value of the file buffer variable at the end of the specified sequential-file or direct-access file. You can use LOCATE to position a direct-access file and then use PUT to write the value of the file buffer variable at that position. After execution of the PUT procedure, the value of the file buffer variable becomes undefined ( UFB returns TRUE ). EOF remains TRUE and the file remains in generation mode.

You can call the PUT procedure for a keyed-access file, regardless of the file's mode (inspection, generation, or undefined). PUT causes the file buffer variable to be written to the file at the position indicated by the key. If the component has more than one key, the file buffer variable is inserted in each index at the appropriate location. After execution of PUT, a keyed-access file is in generation mode.

Consider the following example:


PROGRAM Book_File( INPUT, OUTPUT, Books );
TYPE
   My_String = PACKED ARRAY[1..40] OF CHAR;
   Book_Rec  = RECORD
      Author : My_String;
      Title  : My_String;
      END;
VAR
   New_Book : Book_Rec;
   Books : FILE OF Book_Rec;
   n : INTEGER;
{In the executable section:}
REWRITE( Books );
FOR n := 1 TO 10 DO
   BEGIN
   WITH New_Book DO
      BEGIN
      WRITE( 'Title:' );
      READLN( Title );
      WRITE( 'Author:' );
      READLN( Author );
      END;
   Books^ := New_Book;
   PUT( Books );
   END;

CLOSE( Books );

This program writes the first 10 components read from the terminal into the file Books. The component data items are typed at the terminal and assigned to the record variable New_Book. They consist of two 40-character strings denoting a book's author and title. The FOR loop accepts 10 values for New_Book, assigning each new record to the file buffer variable Books^. The PUT statement writes the value of Books^ into the file for each input record.

For More Information:

9.8.14 READ Procedure

The READ procedure reads one or more file components into a variable.


   READ( [[file_variable,]] {variable-identifier [[:radix-specifier]]},...
         [[, ERROR := error-recovery]] );

file_variable

The name of the file variable associated with the input file. If you omit the name of the file, the default is INPUT.

variable-identifier

The name of the variable into which a file component will be read; multiple identifiers must be separated with commas.

radix-specifier

One of the format values BIN, OCT, or HEX. These values, when used on a variable identifier, will read the variable in binary, octal, or hexadecimal radix respectively. You can use a radix specifier only when reading from a TEXT file.

error-recovery

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

The file must be in inspection mode before READ is called. The file remains in inspection mode after execution of a READ procedure.

The READ procedure for a nontext file performs an assignment statement, a GET procedure, and an UNLOCK procedure for each variable.

Consider the following example:


{This call to READ...}
READ( file_variable, variable-identifier );

{...is equivalent to the following code:}
variable-identifier := file_variable^;
GET( file_variable );

UNLOCK( file_variable );

The READ procedure reads from the file until it has found a value for each variable in the list. The first value read is assigned to the first variable in the list, the second value read is assigned to the second variable, and so on. The values and the variables must be of assignment-compatible types. Reading stops if an error occurs.

For a TEXT file, more than one component (character) can be read into a single variable. For example, many characters can be read into a string or converted into a numeric variable. The READ procedure repeats the assignment, GET, and UNLOCK until it has read a sequence of characters that represent a legal value for the next variable in the parameter list. The procedure continues to read components from the file until it has assigned a value to each variable in the list.

After the last character has been read from a line of a TEXT file, EOLN returns TRUE and the file buffer variable contains a space. Unless you are reading into a character or string variable, a call to READ at this point skips over the end-of-line marker and positions the file at the beginning of the next line. If you are reading into a variable of type CHAR when EOLN returns TRUE, the space is read and assigned to the variable, and the file position advances. If you are reading into a string variable when EOLN becomes TRUE, the file position does not change. In the latter case, you should use the READLN procedure to advance the file position past the end-of-line marker.

Values from a TEXT file can be read into variables of integer, real, Boolean, character, string, and enumerated types. TEXT file values to be read into integer, real, Boolean, and enumerated variables can be preceded in the file by any number of spaces, tabs, and end-of-line markers. Values to be read into character variables, however, must not be separated because they are read and assigned character by character.

In a TEXT file, when HP Pascal encounters a character that forms an object of a data type that does not match the data type of the parameter, reading stops. Consider the following example:


VAR
   i : INTEGER;
{In the executable section:}
READ( i );

If the object in the input file is 123ABC, the read stops at the character 'A', and i contains the value 123.

When reading constant identifiers of an enumerated type from a TEXT file, HP Pascal reads all characters in the identifier, but recognizes only the first 31 characters. You need input only enough characters to make the identifier unique among the other constant identifiers of its type; text input data for enumerated types can consist of both lowercase and uppercase characters.

Boolean input data in TEXT files follow the same rules as other enumerated types. For example, the following character combinations, all of which could appear in a TEXT file, are equivalent: TRUE, True, T, t, tr.

When using a radix specifier, values from a TEXT file can be read into a variable of any type, except a type containing a file component. If the input stream does not provide sufficient data, the high-order bits are set to zero. When reading structured types, the input stream must account for any padding required for alignment.

You can use the READ procedure to read a sequence of characters from a TEXT file into a variable of type PACKED ARRAY OF CHAR. Successive characters from the file are assigned to components of the array, in order, until each component has been assigned a value. If any characters remain on the line after the array is full, the next READ procedure begins with the next character on that line. If the end of the line is encountered before the array is full, spaces are assigned to the remaining components.

You can also read TEXT file characters into a variable of types STRING or VARYING OF CHAR. Characters are assigned to a STRING or VARYING OF CHAR variable in a manner similar to that in which they are assigned to a packed array. However, if the end-of-line marker is encountered before the STRING or VARYING OF CHAR variable has been filled to its maximum length, the STRING or VARYING OF CHAR value is not padded with spaces. Instead, its current length is set equal to the number of characters that have been read into it. If you call the READ procedure with a parameter of type STRING or VARYING OF CHAR, and EOLN returns TRUE, no characters are read into the STRING or VARYING OF CHAR variable; its current length is set to zero.

Every nonempty TEXT file ends with an end-of-line marker and an end-of-file marker. Therefore, EOF never becomes TRUE when you are reading strings with the READ procedure. To test EOF when reading strings, use a READLN procedure to advance the file beyond the end-of-line marker.

Consider the following example:


READ( Temp, Age, Weight );

Assume that Temp, Age, and Weight are real variables, and that the following values have been entered at the terminal:


98.6 11 75

The variable Temp is assigned the value 98.6, Age is assigned the value 11.0, and Weight is assigned the value 75.0. You need not type all three values on the same line.

Consider the following example:


TYPE
   A_String = PACKED ARRAY[1..20] OF CHAR;
VAR
   Names      : TEXT;
   Pres, Veep : A_String;
{In the executable section:}
READ( Names, Pres, Veep );

This program fragment declares and reads the file Names, which contains the following character strings:


John F. Kennedy     Lyndon B. Johnson   Lyndon B. Johnson   <EOLN>
Hubert H. Humphrey  <EOLN>
Richard M. Nixon    Spiro T. Agnew      <EOLN>

The first call to the READ procedure sets Pres equal to the 20-character string 'John F. Kennedy ' and Veep equal to 'Lyndon B. Johnson '. The second call to the procedure assigns the value 'Lyndon B. Johnson ' to Pres and, after encountering the end-of-line marker, fills the array Veep with spaces. The file position does not advance to the beginning of the next line until a READLN is performed.

You can abbreviate values to be read into enumerated variables, as shown in the next example:



TYPE
   Color = ( Red, Fire_Engine_Green, Blue, Black );
VAR
   Light : Color;
{In the executable section:}
READ( Light );

In this example, if the letter R is read, the variable Light is assigned the value Red. However, if the letters Redx are read, an error occurs. If the letters Bl are read, an error also occurs because Bl is not unique. However, the letters Blu are unique and would be interpreted as the constant identifier Blue.

For More Information:


Previous Next Contents Index