HP OpenVMS Systems Documentation

Content starts here

HP Pascal for OpenVMS
Language Reference Manual

Previous Contents Index

9.3 Component Access Modes

A component access mode is a method by which HP Pascal retrieves components from a file. You cannot change the file organization or component format after file creation, but you can change the component access mode each time you access a file. The HP Pascal I/O model defines two component access modes: sequential and random access. Random access can be further broken down into the categories of random access by number (also called direct access) and random access by key value (also called keyed access). The following sections describe these access methods in further detail.

You specify the access method using the OPEN procedure when you open a file. You cannot change the access method unless you first use the CLOSE routine, and then reopen the file specifying a new access method.

Before trying to use any of the access methods on a file, HP Pascal determines the organization of the file. The organization determines how the specified access method works. For instance, sequential access on a sequentially organized file works differently than sequential access on an indexed file.

Default Information:

  • The default is the sequential access method.
  • You can always process a file using sequential access, even when the currently specified access method is one of the direct access methods.
  • By default, HP Pascal does not designate a component as a starting point for access; you must do this explicitly using one of the RESET, REWRITE, or REWIND procedures, or using an access-specific procedure to locate a specified component.

Table 9-4 shows which file organizations support which component access modes.

Table 9-4 File Organization Support for Component Access Modes
Access Mode Sequential
Sequential Yes Yes Yes
Random by relative
component number
(direct access)
Yes 1 Yes No
Random by key
(keyed access)
No No Yes

1This access is permitted with a fixed-length component format on disk only.

For More Information:

9.3.1 Sequential Access

Using the sequential access method, storage or retrieval begins at a designated position in the file and continues through the file according to the component's position in storage. You can specify two starting points for sequential access: the beginning of the file (using REWRITE, or RESET) or the end of the file (using EXTEND).

The following are the HP Pascal I/O routines that are used for sequential access:


For More Information: Sequential Access to Sequential Files

To retrieve a component in a sequential file, you must retrieve all components from the time you establish a current position (using either EXTEND, RESET, or REWRITE) to the desired component. After an operation to the file, HP Pascal positions the file pointer to the next file component in anticipation of the next operation on the file.

To access a previous component, you must reopen (implicitly or explicitly) and reread the previous components; or, you can reopen the file, switching to random access mode.

You cannot add components in between any two components. You can add components only to the current end of the file.

Figure 9-6 shows sequential access to sequential files.

Figure 9-6 Sequential Access to a Sequential File Sequential Access to Relative Files

HP Pascal can use sequential access for relative files as long as the components are fixed length. HP Pascal tries to store or retrieve from the cell whose relative component number is one higher than the most recently accessed cell.

You cannot overwrite a component, but you can modify the contents of the current component. If the cell with the next highest relative component number contains a component and if you are trying to store data in that cell, you generate an error.

Figure 9-7 shows the use of sequential access to read from a relative file.

Figure 9-7 Using Sequential Access to Read from a Relative File

Figure 9-8 shows the use of sequential access to write to a relative file. In this figure, HP Pascal writes the component to the current cell. If the program requests that another component be stored sequentially, then HP Pascal places that component in cell 3. If the program places another request to store a component sequentially, an error occurs because cell 4 contains component B.

Figure 9-8 Using Sequential Access to Write to a Relative File Sequential Access to Indexed Files

When sequentially accessing an indexed file, HP Pascal uses a specified index to determine the order in which to sequentially process the file components. The specified keys are called the keys of reference.

If you specify ACCESS_METHOD := SEQUENTIAL when you open an indexed file, you can only access components sequentially according to the primary key. If you specify ACCESS := KEYED when you open an indexed file, you can access components sequentially according to any key.

When sequentially writing components to an indexed file, HP Pascal stores the component according to the primary key. If your program uses secondary keys, HP Pascal updates the secondary key pointers to include the newly stored component.

9.3.2 Random Access

Random access allows you to access file components in an order that is not dependent on the file organization or on the order in which the components are stored. Random access is available for all relative and indexed files, and for sequential files composed of fixed-length components (the fixed-length components allow HP Pascal to count component positions in the sequential file without having to worry about variations in the lengths of the components).

HP Pascal supports the following types of random access:

  • Random access by relative component number (direct access)
  • Random access by key value (keyed access)

The following are the HP Pascal I/O routines that are used for random access:

Random access by relative component number:
Random access by key:

For More Information: Random Access by Relative Component Numbers (Direct Access)

HP Pascal supports random access by relative component numbers for relative files and for sequential files with fixed-length components on disk. To access the desired component, you need to specify the relative component number of the corresponding cell; relative component numbers are relative to the beginning of the file.

Figure 9-9 shows the process of randomly accessing cells in a file. For random access of sequential files, the cells must be of a fixed length.

Figure 9-9 Using Random Access on Sequential and Relative Files Random Access to Indexed Files (Keyed Access)

HP Pascal supports random access to indexed files. To retrieve a component, you must specify an index (primary index, first alternate index, second alternate index, and so forth) and a key value. To store a component, HP Pascal determines existing keys from the file organization and stores the record (and alternate key information) according to information as it exists in the data portion of the component.

Your program can use several methods to randomly access a record by key:

  • Exact match of key values.
  • Approximate match of key values. When accessing an index in ascending sort order, HP Pascal returns the component that has the next higher key value (in descending order, the component with next lower key value).
  • Generic match of key values. Generic matching is applicable to string data-type keys only (PACKED ARRAY OF CHAR record fields). For a generic match, the program need specify only a match of some specified number of leading characters in the key.
  • Combination of approximate and generic match.

9.4 File Locking

Under some circumstances, if a file component is in the process of being read or written to by one program, HP Pascal locks the component, preventing other programs from accessing the component. This prevents programs from accessing outdated or inaccurate data.

If you OPEN a file and specify that the file is not to be shared, or that reading or writing sharing is allowed, HP Pascal may not lock the record.

Record locking occurs most often when accessing relative and indexed files, but it can happen when accessing sequential files as well. Successful calls to FIND, FINDK, GET, RESET, and RESETK lock the current component. If you want to make a locked file component available to other programs on the system, you can call the UNLOCK procedure.

For More Information:

9.5 TEXT Files

Files of type TEXT are sequences of characters with special markers (end-of-line and end-of-file) added to the file. Although each character of a TEXT file is one file component, the end-of-line marker allows you to process the file line by line (using READLN, WRITELN, or EOLN), if you choose.

The predeclared file variables INPUT and OUTPUT are files of type TEXT. They refer to the standard input and output files. (When executing programs at a terminal, INPUT and OUTPUT default to the terminal you are using.)

The file type FILE OF CHAR differs from TEXT files in that FILE OF CHAR allows a single character to be the unit of transfer between a program and its associated I/O devices, and that FILE OF CHAR files do not include special markers. FILE OF CHAR components are always read with the READ procedure, and must be read exclusively into variables of type CHAR, including CHAR components of structured variables. You cannot use the EOLN, READLN, and WRITELN routines on FILE OF CHAR files.

Default Information:

  • A new file of type TEXT or FILE OF VARYING OF CHAR is a sequential file with variable-length components.
  • All TEXT file routines use the predefined files INPUT and OUTPUT by default.
  • HP Pascal performs an implicit call to RESET on the predeclared file INPUT and an implicit call to REWRITE on the predeclared file OUTPUT.
  • The default size for the output buffer is 255 characters for TEXT files.

The following are the HP Pascal I/O routines that are used only with TEXT files: EOLN, LINELIMIT, PAGE, READLN, and WRITELN.

9.5.1 Carriage Control

Some devices, such as printers and terminals, are carriage-control devices and require characters to provide information regarding output. HP Pascal supports the following carriage-control options:

OPEN Parameter
LIST Single spacing between components. This is the default carriage-control option for all TEXT files (including OUTPUT) and VARYING OF CHAR files.
CARRIAGE, FORTRAN The first character of every output line is a carriage-control character.
NONE, NOCARRIAGE No carriage control. This is the default for all files other than TEXT and VARYING OF CHAR files.

For FORTRAN carriage control, if output is directed to devices that do not use carriage-control characters, the character is written into the file as a component and is read back when the file is opened for input. If output is directed to devices that do use carriage control, then the OPEN parameter options described previously determine the action taken by HP Pascal.

Table 9-5 summarizes carriage-control characters and their effects. For purposes of carriage control, HP Pascal ignores any characters other than those listed in the table.

Table 9-5 Carriage-Control Characters
Character Meaning
'+' Overprinting: starts output at the beginning of the current line.
' ' Single spacing: starts output at the beginning of the next line.
'0' Double spacing: skips a line before starting output.
'1' Paging: starts output at the top of a new page.
'$' Prompting: starts output at the beginning of the next line and suppresses carriage return at the end of the line.
''(0) Prompting with overprinting: suppresses line feed at the beginning of the line and carriage return at the end of the line; note that this character is the ASCII character NUL.

9.5.2 Prompting on a Terminal

Normally, when you call the WRITE procedure to access a TEXT file connected to a terminal, HP Pascal accumulates the characters in a line buffer until a subsequent WRITELN procedure is executed. In effect, WRITELN generates an end-of-line marker. When you complete a line or close a file, HP Pascal writes a full line of characters to the specified TEXT file.

HP Pascal can manipulate partial lines in a TEXT file; however, when characters are being written to a terminal output file opened with the LIST carriage-control option (LIST is the default), partial lines are written to the terminal before input is transferred from any terminal to the line buffer of a TEXT file. In this situation, HP Pascal searches for all TEXT files opened for output on terminals; it then writes to those files any partial lines contained in the files' respective line buffers. These partial lines, called prompts, appear on the screen. You respond to a prompt by typing a line of input data terminated by pressing Return.

Consider the following example:

WRITE( 'Name three presidents:' );
READ( Pres1, Pres2, Pres3);

HP Pascal stores the string 'Name three presidents:' in the output buffer; when executing the READ procedure, HP Pascal locates the TEXT file opened for output to the appropriate terminal and the partial output buffer is written, causing the string 'Name three presidents:' to appear on the terminal screen. The user can then begin typing on the same line as the prompt, providing the names of three presidents. Note that prompting works only for files associated with interactive terminals. For any other files, HP Pascal does not write output until you start the new line with a WRITELN procedure.

9.5.3 Delayed Device Access to Text Files

The Pascal standard requires that the file buffer always contain the next file component that will be processed by the program. This definition can cause problems when the input to the program depends on the output most recently generated. To alleviate such problems in the processing of the TEXT files, HP Pascal uses a technique called delayed device access, also known as lazy lookahead.

As a result of delayed device access, HP Pascal does not retrieve an item of data from a physical file device and does not insert it in the file buffer until the program is ready to process it. HP Pascal fills the file buffer when the program makes the next reference to the file. A reference to the file consists of any use of the file buffer variable, including its implicit use in the GET, READ, and READLN procedures, or any test for the status of the file, namely, the EOF, EOLN, STATUS, and UFB functions.

The RESET procedure, which is required when any TEXT file is opened for input, initiates the process of delayed device access. (Note that RESET is done automatically on the predeclared file INPUT.) RESET expects to fill the file buffer with the first component of the file. However, because of delayed device access, an item of data is not supplied from the input device to fill the file buffer until the next reference to the file.

When writing a program for which the input will be supplied by a TEXT file, you should be aware that delayed device access occurs. Because RESET initiates delayed device access, and because EOF and EOLN cause the file buffer to be filled, you should place the first prompt for input before any tests for EOF or EOLN. The information you enter in response to the prompt supplies data that is retained by the file device until you make another reference to the input file.

Consider the following example:

{In the executable section:}
WRITE( 'Enter an integer or an empty line: ' );
    READLN( i );
    WRITELN( 'The integer was: ', i:1 );
    WRITE( 'Enter an integer or an empty line: ' );
WRITELN( 'Done' );

The first reference to the file INPUT is the EOLN test in the WHILE statement. When the test is performed, HP Pascal attempts to read a line of input from the TEXT file. Therefore, it is very important to prompt for the integer or empty line before testing for EOLN.

Suppose you respond to the first prompt by supplying an integer as input. Access to the input device is delayed until the EOLN function makes the first reference to the file INPUT. The EOLN function causes a line of text to be read into the internal line buffer. The subsequent READLN procedure reads the input value from the line of text and assigns it to the variable i. The WRITELN procedure writes the input value to the text file OUTPUT. The final statement in the WHILE loop is the request for another input value. The loop terminates when EOLN detects the end-of-line marker.

A sample run of a program containing this loop might be as follows:

Enter an integer or an empty line: 10
The integer was: 10
Enter an integer or an empty line: 99
The integer was: 99
Enter an integer or an empty line: [Return]

The following program fragment shows a method of writing the same loop that does not take into account delayed device access so it produces incorrect results:

WRITE( 'Enter an integer or an empty line: ' );
READLN( i );
WRITELN( 'The integer was: ', i:1 );

The EOLN test at the beginning of the loop causes the file buffer to be filled. However, because no input has been supplied yet, the prompt does not appear on the screen until you have supplied input to fill the INPUT file buffer.

A sample run of a program containing this loop might be as follows:

Enter an integer or an empty line: The integer was: 10
Enter an integer or an empty line: The integer was: 99

The prompt always appears after you type a value for i.

Delayed device access can produce unexpected results if you try to use the STATUS function to test the status of a TEXT file after you have performed a READLN procedure on the file. Remember that a READLN procedure call actually performs a READ procedure on each variable listed as a parameter, then performs a READLN procedure to position the file at the beginning of the next line. Therefore, a call to STATUS after a READLN procedure actually tests whether the file was successfully positioned. To test the status of the file, STATUS causes delayed device access to occur, which fills the file buffer with the next component. If you want to test the successful reading of data from the input file, read the data with the READ procedure, call the STATUS function, and then perform a READLN procedure to advance the file to the beginning of the next line.

Previous Next Contents Index