Compaq Fortran
User Manual for
OpenVMS Alpha Systems


Previous Contents Index

6.4.2 Internal Files and Scratch Files

Compaq Fortran also supports two other types of files that are not file organizations---namely, internal files and scratch files.

Internal Files

You can use an internal file to reference character data in a buffer when using sequential access. The transfer occurs between internal storage and internal storage (unlike external files), such as between character variables and a character array.

An internal file consists of any of the following:

Instead of specifying a unit number for the READ or WRITE statement, use an internal file specifier in the form of a character scalar memory reference or a character-array name reference.

An internal file is a designated internal storage space (variable buffer) of characters that is treated as a sequential file of fixed-length records. To perform internal I/O, use formatted and list-directed sequential READ and WRITE statements. You cannot use such file-related statements such as OPEN and INQUIRE on an internal file (no unit number is used).

If an internal file is made up of a single character variable, array element, or substring, that file comprises a single record whose length is the same as the length of the variable, array element, or substring. If an internal file is made up of a character array, that file comprises a sequence of records, with each record consisting of a single array element. The sequence of records in an internal file is determined by the order of subscript progression.

A record in an internal file can be read only if the character variable, array element, or substring comprising the record has been defined (a value has been assigned to the record).

Prior to each READ and WRITE statement, an internal file is always positioned at the beginning of the first record.

Scratch Files

Scratch files are created by specifying STATUS='SCRATCH' on an OPEN statement. By default, the files are created on the user's default disk (SYS$DISK) and are not placed in a directory or given a name that is externally visible (accessible using the DCL command DIRECTORY).

You can create scratch files on a disk other than the default disk by using the FILE specifier in an OPEN statement.

6.4.3 I/O Record Types

Record type refers to whether records in a file are all the same length, are of varying length, or use other conventions to define where one record ends and another begins.

You can use fixed-length and variable-length record types with sequential, relative, or indexed files. You can use any of the record types with sequential files.

Records are stored in one the following record types:

You can use fixed-length and variable-length record types with sequential, relative, or indexed files.

Before you choose a record type, consider whether your application will use formatted or unformatted data. If you will be using formatted data, you can use any record type except segmented. When using unformatted data, you should avoid using the stream, stream_CR, and stream_LF record types.

The segmented record type is unique to Compaq Fortran products; it is not used by other OpenVMS-supported languages. It can only be used for unformatted sequential access with sequential files. You should not use segmented records for files that are read by programs written in languages other than Fortran.

The stream, stream_CR, stream_LF, and segmented record types can only be used with sequential files.

6.4.3.1 Portability Considerations of Record Types

Consider the following portability needs when choosing a record type:

Compaq Fortran indexed files are portable only to other OpenVMS systems. However, a conversion program can read the records from an indexed file and write them to another file, such as a sequential (or relative) file.

6.4.3.2 Fixed-Length Records

When you create a file that uses the fixed-length record type, you must specify the record size. When you specify fixed-length records, all records in the file must contain the same number of bytes. (The Compaq Fortran Language Reference Manual discusses fixed-length records.)

A sequential file opened for direct access must contain fixed-length records, to allow the record position in the file to be computed correctly.

You can obtain the record length (RECL) before opening the file with unformatted data by using a form of the INQUIRE statement (see Section 6.6.3).

6.4.3.3 Variable-Length Records

Variable-length records can contain any number of bytes, up to a specified maximum. These records are prefixed by a count field, indicating the number of bytes in the record. The count field comprises two bytes on a disk device and four bytes on magnetic tape. The value stored in the count field indicates the number of data bytes in the record.

Variable-length records in relative files are actually stored in fixed-length cells, the size of which must be specified by means of the RECL specifier in an OPEN statement (see the Compaq Fortran Language Reference Manual for details). This RECL value specifies the largest record that can be stored in the file.

The count field of a formatted variable-length record is available when you read the record by issuing a READ statement with a Q format descriptor. You can then use the count field information to determine how many bytes should be in an I/O list.

6.4.3.4 Segmented Records

A segmented record is a single logical record consisting of one or more variable-length, unformatted records in a sequential file. Each variable-length record constitutes a segment. The length of a segmented record is arbitrary.

Segmented records are useful when you want to write exceptionally long records but cannot or do not wish to define one long variable-length record. When writing unformatted data to a sequential file using sequential access, the default record type is segmented.

As shown in Figure 6-1, the layout of segmented records consists of control information followed by the user data. On disk, the control information consists of four bytes for compatibility with other Compaq Fortran platforms. However, OpenVMS RMS removes the first two length bytes when the record is read, so each record has two control bytes (flags) in memory.

Figure 6-1 Segmented Records


The control information consists of a 2-byte integer record size count (includes the two bytes used by the segment identifier), followed by a 2-byte integer segment identifier that identifies this segment as one of the following:
Identifier Value Segment Identified
0 One of the segments between the first and last segments.
1 First segment.
2 Last segment.
3 Only segment.

When you wish to access an unformatted sequential file that contains variable-length records, you must specify FORM='UNFORMATTED' when you open the file. If the unformatted data file was not created using a Compaq Fortran product, specify RECORDTYPE='VARIABLE'. If the unformatted data file was created using the segmented record type using a Compaq Fortran product, specify RECORDTYPE='SEGMENTED'.

Otherwise, the first two bytes of each record will be mistakenly interpreted as control information, and errors will probably result.

You can obtain the record length (RECL) before opening the file with unformatted data using a form of the INQUIRE statement (see Section 6.6.3).

6.4.3.5 Stream Records

A stream record is a variable-length record whose length is indicated by explicit record terminators embedded in the data, not by a count.

Stream files use the 2-character sequence consisting of a carriage-return and a line-feed as the record terminator. These terminators are automatically added when you write records to a stream file and removed when you read records.

Stream records resemble the Stream_CR or Stream_LF records shown in Figure 6-2, but use a 2-byte record terminator (carriage-return and line-feed) instead of a 1-byte record terminator.

6.4.3.6 Stream_CR and Stream_LF Records

A Stream_CR or Stream_LF record is a variable-length record whose length is indicated by explicit record terminators embedded in the data, not by a count. These terminators are automatically added when you write records to a stream-type file and are removed when you read records.

Each variety uses a different 1-byte record terminator:

The layout of Stream_CR and Stream_LF records appears in Figure 6-2.

Figure 6-2 Stream_CR and Stream_LF Records


6.4.4 Other File Characteristics

Other file characteristics include:

The units used for specifying record length depend on the form of the data:

For More Information:

6.5 Opening Files and the OPEN Statement

You can choose to open files by:

If you choose to specify a logical name with the FILE specifier in an OPEN statement, that logical name must be associated with a file specification and the character expression specified for the logical name must contain no punctuation marks.

6.5.1 Preconnected Files and Fortran Logical Names

You can use OpenVMS logical names to associate logical units with file specifications. A logical name is a string up to 255 characters long that you can use as part of a file specification.

Table 6-3 lists the OpenVMS process logical names for standard I/O devices already associated with particular file specifications.

Table 6-3 Predefined System Logical Names
OpenVMS Logical Name Meaning Default
SYS$COMMAND Default command stream For an interactive user, the default is the terminal; for a batch job, the default is the batch job input command file.
SYS$DISK Default disk device As specified by user.
SYS$ERROR Default error stream For an interactive user, the default is the terminal; for a batch job, the default is the batch job log file.
SYS$INPUT Default input stream For an interactive user, the default is the terminal; for a batch job, the default is the batch command file.
SYS$OUTPUT Default output stream For an interactive user, the default is the terminal; for a batch job, the default is the batch log file.

You can dynamically create a logical name and associate it with a file specification by means of the DCL commands ASSIGN or DEFINE. For example, before program execution, you can define each logical name recognized by your program with a file specification appropriate to your needs without recompiling and relinking the program. For example:


$ DEFINE LOGNAM USERD:[SMITH]TEST.DAT;2

The preceding command creates the logical name LOGNAM and associates it with the file specification USERD:[SMITH]TEST.DAT;2. As a result, this file specification is used whenever the logical name LOGNAM is encountered during program execution.

The following statement opens the file associated with the current definition of the logical name LOGNAM:


  OPEN (UNIT=7, FILE='LOGNAM', STATUS='OLD') 

Logical names provide great flexibility because they can be associated with either a partial or complete file specification (with either a device or a device and a directory), or even another logical name.

6.5.1.1 Preconnected Files

ACCEPT, TYPE, and PRINT statements do not refer explicitly to a logical unit (a file or device) from which or to which data is to be transferred; they refer implicitly to a default preconnected logical unit. ACCEPT refers to the default input device SYS$INPUT. TYPE and PRINT refer to the default output device SYS$OUTPUT. These defaults can be overridden with appropriate logical name assignments (see Section 6.5.1.2).

READ, WRITE, and REWRITE usually refer to explicit unit numbers. If you do not use an OPEN statement to open logical unit 5 or 6 without setting the appropriate logical name (FORnnn), unit number 5 is associated with SYS$INPUT and unit 6 with SYS$OUTPUT.

At run time, if units 5 and 6 are specified by a record I/O statement (such as READ or WRITE) without having been explicitly opened by an OPEN statement, Compaq Fortran implicitly opens units 5 and 6 and associates them with their respective operating system standard I/O files if the corresponding logical name is not set.

To redirect I/O to an external disk file instead of these preconnected files, you can either use an OPEN statement to unit 5 and 6 or set the appropriate logical name. If you set the corresponding Compaq Fortran logical name, the file specified by that Compaq Fortran logical name is used.

The order of precedence when you open a file is:

Table 6-4 shows the I/O statements and their associated Fortran logical names and OpenVMS standard I/O logical names.

Table 6-4 Implicit Fortran Logical Units
Statement Fortran Logical Name1 Equivalent OpenVMS Logical Name1
READ (*,f) iolist FOR$READ SYS$INPUT
READ f,iolist FOR$READ SYS$INPUT
ACCEPT f,iolist FOR$ACCEPT SYS$INPUT
WRITE (*,f) iolist FOR$PRINT SYS$OUTPUT
PRINT f,iolist FOR$PRINT SYS$OUTPUT
TYPE f,iolist FOR$TYPE SYS$OUTPUT
READ (5),iolist FOR005 SYS$INPUT
WRITE (6),iolist FOR006 SYS$OUTPUT


1If the Fortran logical name is defined, it is used; if the Fortran logical name is not defined, the OpenVMS standard I/O logical names are used.

You can change the file specifications associated with these Fortran logical names by using the DCL commands DEFINE or ASSIGN.

6.5.1.2 Compaq Fortran Logical Names

Compaq Fortran I/O is usually performed by associating a logical unit number with a device or file. OpenVMS logical names provide an additional level of association; a user-specified logical name can be associated with a logical unit number.

Compaq Fortran provides predefined logical names in the following form:

FORnnn

The notation nnn represents a logical unit number, any non-negative 4-byte integer (maximum value is 2,147,483,647). For example, for logical unit 12, the predefined logical name would be FOR012; for logical unit 1024, the predefined logical name would be FOR1024.

By default, each Fortran logical name is associated with a file named FORnnn.DAT on your default disk under your default directory. For example:


  WRITE (17,200) 

If you enter the preceding statement without including an explicit file specification, the data is written to a file named FOR017.DAT on your default disk under your default directory.

You can change the file specification associated with a Fortran logical unit number by using the DCL commands ASSIGN or DEFINE to change the file associated with the corresponding Fortran logical name. For example:


$ DEFINE FOR017 USERD:[SMITH]TEST.DAT;2 

The preceding command associates the Fortran logical name FOR017 (and therefore logical unit 17) with file TEST.DAT;2 on device USERD in directory [SMITH].

You can also associate the Fortran logical names with any of the predefined system logical names, as shown in the following examples:

For More Information:

On the DCL commands you can use to assign or deassign logical names, see Appendix C.

6.5.2 Disk Files and File Specifications

Most I/O operations involve a disk file, keyboard, or screen display. You can access the terminal screen or keyboard by using preconnected files, as described in Section 6.5. Otherwise, this chapter discusses disk files.

Compaq Fortran recognizes logical names for each logical I/O unit number in the form of FORnnn, where nnn is the logical I/O unit number, with leading zeros for fewer than three digits. If a file name is not specified in the OPEN statement and the corresponding FORnnn logical name is not set for that unit number, Compaq Fortran generates a file name in the form FORnnn.DAT, where n is the logical unit number.

Certain Compaq Fortran logical names are recognized and preconnected files exist for certain unit numbers. Performing an implied OPEN means that the FILE and DEFAULTFILE specifier values are not specified and a logical name is used, if present.

A complete OpenVMS file specification has the form:

node::device:[directory]filename.filetype;version

For example:


  BOSTON::USERD:[SMITH]TEST.DAT;2 

You can associate a file specification with a logical unit by using a logical name assignment (see Section 6.5.1) or by using an OPEN statement (see Section 6.5.2). If you do not specify such an association or if you omit elements of the file specification, the system supplies default values, as follows:

For example, if your default device is USERD and your default directory is SMITH, and you specified the following statements:


  READ (8,100) 
  . 
  . 
  . 
  WRITE (9,200) 

The default input file specification would be:

USERD:[SMITH]FOR008.DAT;n

The default output file specification would be:

USERD:[SMITH]FOR009.DAT;m

In these examples, n equals the highest current version number of FOR008.DAT and m is 1 greater than the highest existing version number of FOR009.DAT.

You can use the FILE and DEFAULTFILE specifiers in an OPEN statement to specify the complete definition of a particular file to be opened on a logical unit. For example:


  OPEN (UNIT=4, FILE='USERD:[SMITH]TEST.DAT;2', STATUS='OLD') 

In the preceding example, the existing file TEST.DAT;2 on device USERD in directory SMITH is to be opened on logical unit 4. Neither the default file specification (FOR004.DAT) nor the Fortran logical name FOR004 is used. The value of the FILE specifier can be a character constant, variable, or expression.

Compaq Fortran provides the following possible ways of specifying all or part of a file specification (directory and file name), such as DISK2:[PROJECT.DATA]:

In the following interactive example, the file name is supplied by the user and the DEFAULTFILE specifier supplies the default values for the file specification string. The file to be opened is in device and directory DISK4:[PROJ] and is merged with the file name typed by the user into the variable DOC:


     CHARACTER(LEN=40) DOC 
     WRITE (6,*)  'Type file name ' 
     READ (5,*) DOC 
     OPEN (UNIT=2, FILE=DOC, DEFAULTFILE='DISK4:[PROJ]',STATUS='OLD') 

The DEFAULTFILE specification overrides your process default device and directory.

You can also specify a logical name as the value of the FILE specifier, if the logical name is associated with a file specification. If the logical name LOGNAM is defined to be the file specification USERD:[SMITH]TEST.DAT, the logical name can then be used in an OPEN statement, as follows:


  OPEN (UNIT=19, FILE='LOGNAM', STATUS='OLD') 

When an I/O statement refers to logical unit 19, the system uses the file specification associated with logical name LOGNAM.

If the value specified for the FILE specifier has no associated file specification, it is regarded as a true file name rather than as a logical name. Suppose LOGNAM had not been previously associated with the file specification by using an ASSIGN or DEFINE command. The OPEN statement would indicate that a file named LOGNAM.DAT is located on the default device, in the default directory.

For an example program that reads a typed file name, uses the typed name to open a file, and handles such errors as the "file not found" error, see Example 7-1.

For a detailed description of OpenVMS file specifications, see the Guide to OpenVMS File Applications.

For More Information:


Previous Next Contents Index