Previous | Contents | Index |
The OPEN statement connects a unit number with an external file and allows you to explicitly specify file attributes and run-time options using OPEN statement specifiers. Once you open a file, you should close it before opening it again unless it is a preconnected file.
If you open a unit number that was opened previously (without being closed), one of the following occurs:
You can use the INQUIRE statement (see Section 6.6) to obtain information about a whether or not a file is opened by your program.
Especially when creating a new file using the OPEN statement, examine the defaults (see the description of the OPEN statement in the Compaq Fortran Language Reference Manual) or explicitly specify file attributes with the appropriate OPEN statement specifiers.
Table 6-5 lists the OPEN statement functions and their specifiers.
Category, Functions, and OPEN Statement Specifiers | |
---|---|
Identify File and Unit | |
UNIT specifies the logical unit number. | |
FILE (or NAME 1) and DEFAULTFILE 1 specify the directory and/or file name of an external file. | |
STATUS or TYPE 1 indicates whether to create a new file, overwrite an existing file, open an existing file, or use a scratch file. | |
STATUS or DISPOSE 1 specifies the file existence status after CLOSE. | |
File and Record Characteristics | |
ORGANIZATION 1 indicates the file organization (sequential, relative, or indexed). | |
RECORDTYPE 1 indicates which record type to use. | |
FORM indicates whether records are formatted or unformatted. See Section 6.4.4 and Section 6.3. | |
CARRIAGECONTROL 1 indicates the terminal control type. | |
KEY 1 indicates (when creating an indexed file) the key number, its type, and its location. | |
NOSPANBLOCKS 1 indicates that the records should not span block boundaries. | |
RECL or RECORDSIZE 1 specifies the record size. See Section 6.4.4. | |
Special File Open Routine | |
USEROPEN 1 names the routine that will open the file to establish special context that changes the effect of subsequent Compaq Fortran I/O statements (see Chapter 11). | |
File Access, Processing, and Position | |
ACCESS indicates the access mode (direct, keyed, or sequential). See Section 6.8.2. | |
ACTION or READONLY 1 indicates whether statements will be used to only read records, only write records, or read and write records. See Section 6.8.3. | |
POSITION indicates whether to position the file at the beginning of file, before the end-of-file record, or leave it as is (unchanged). See Section 6.8.4. | |
SHARED 1 indicates that other users can access the same file and activates record locking. See Section 6.8.3. | |
MAXREC 1 specifies the maximum record number for direct access. | |
ASSOCIATEVARIABLE 1 specifies the variable containing next record number for direct access. | |
File Allocation | |
INITIALSIZE 1 indicates the allocation unit (in blocks) when creating a file. | |
EXTENDSIZE 1 indicates the allocation unit (in blocks) when allocation additional file space. | |
Record Transfer Characteristics | |
BLANK indicates whether to ignore blanks in numeric fields. | |
DELIM specifies the delimiter character for character constants in list-directed or namelist output. | |
PAD, when reading formatted records, indicates whether padding characters should be added if the item list and format specification require more data than the record contains. | |
BLOCKSIZE 1 specifies the block physical I/O buffer size. | |
BUFFERCOUNT 1 specifies the number of physical I/O buffers. | |
CONVERT 1 specifies the format of unformatted numeric data. See Chapter 9. | |
Error Handling Capabilities | |
ERR specifies a label to branch to if an error occurs. See Chapter 7. | |
IOSTAT specifies the integer variable to receive the error (IOSTAT) number if an error occurs. See Chapter 7. | |
File Close Action | |
DISPOSE 1 identifies the action to take when the file is closed. |
For More Information:
The INQUIRE statement returns information about a file and has three forms:
An inquiry by unit is usually done for an opened (connected) file. An inquiry by unit causes the Compaq Fortran RTL to check whether the specified unit is connected or not. One of the following occurs:
For example, the following INQUIRE statement shows whether unit 3 has a file connected (OPENED specifier) in logical variable I_OPENED, the name (case sensitive) in character variable I_NAME, and whether the file is opened for READ, WRITE, or READWRITE access in character variable I_ACTION:
INQUIRE (3, OPENED=I_OPENED, NAME=I_NAME, ACTION=I_ACTION) |
An inquiry by name causes the Compaq Fortran RTL to scan its list of open files for a matching file name. One of the following occurs:
The following INQUIRE statement returns whether the file named LOG_FILE is a file connected in logical variable I_OPENED, whether the file exists in logical variable I_EXIST, and the unit number in integer variable I_NUMBER.
INQUIRE (FILE='log_file', OPENED=I_OPEN, EXIST=I_EXIST, NUMBER=I_NUMBER) |
Unlike inquiry by unit or inquiry by name, inquiry by output item list does not attempt to access any external file. It returns the length of a record for a list of variables that would be used for unformatted WRITE, READ, and REWRITE statements (REWRITE is a Compaq Fortran extension).
The following INQUIRE statement returns the maximum record length of the variable list in integer variable I_RECLENGTH. This variable is then used to specify the RECL value in the OPEN statement:
INQUIRE (IOLENGTH=I_RECLENGTH) A, B, H OPEN (FILE='test.dat', FORM='UNFORMATTED', RECL=I_RECLENGTH, UNIT=9) |
For an unformatted file, the RECL value is returned using 4-byte units, unless you specify the /ASSUME=BYTERECL qualifier to request 1-byte units.
For More Information:
Usually, any external file opened should be closed by the same program before it completes. The CLOSE statement disconnects the unit and its external file. You must specify the unit number (UNIT specifier) to be closed.
You can also specify:
To delete a file when closing it:
If you opened an external file and did an inquire by unit, but do not like the default value for the ACCESS specifier, you can close the file and then reopen it, explicitly specifying the ACCESS desired.
There usually is no need to close preconnected units. Internal files are neither opened nor closed.
For More Information:
After you open a file or use a preconnected file, you can use the following statements:
These statements are described in Section 6.2 and the Compaq Fortran Language Reference Manual.
The record I/O statement must use the appropriate record I/O form
(formatted, list-directed, namelist, or unformatted), as described in
Section 6.3.
6.8.1 Record I/O Statement Specifiers
You can use the following specifiers with the READ and WRITE record I/O statements:
When using nonadvancing I/O, use the ADVANCE, EOR, and SIZE specifiers, as described in Section 6.8.5.
When using the REWRITE statement (a Compaq Fortran extension), you can use the UNIT, FMT, ERR, and IOSTAT specifiers.
For More Information
Record access refers to how records will be read from or written to a file, regardless of its organization. Record access is specified each time you open a file; it can be different each time.
The type of record access permitted is determined by the combination of file organization and record type. Access mode is the method a program uses to retrieve and store records in a file. The access mode is specified as part of each I/O statement.
Compaq Fortran supports three record access modes:
Your choice of record access mode is affected by the organization of the file to be accessed. For example, the keyed access mode can be used only with indexed organization files.
Table 6-6 shows all of the valid combinations of access mode and file organization.
File Organization | Record Access Mode | ||
---|---|---|---|
Sequential | Direct | Keyed | |
Sequential | Yes | Yes 1 | No |
Relative | Yes | Yes | No |
Indexed | Yes | No | Yes |
If you select the sequential access mode for sequential or relative files, records are written to or read from the file starting at the beginning and continuing through the file, one record after another. For indexed files, sequential access can be used to read or write all records according to the direction of the key and the key values. Sequential access to indexed files can also be used with keyed access to read or write a group of records at a specified point in the file.
When you use sequential access for sequential and relative files, a particular record can be retrieved only after all of the records preceding it have been read.
Writing records by means of sequential access also varies according to the file organization:
If you select direct access mode, you determine the order in which records are read or written. Each READ or WRITE statement must include the relative record number, indicating the record to be read or written.
You can access relative files directly. You can also access a sequential disk file directly if it contains fixed-length records. Because direct access uses cell numbers to find records, you can issue successive READ or WRITE statements requesting records that either precede or follow previously requested records. The following statements read record 24, then read record 10.
READ (12,REC=24) I READ (12,REC=10) J |
If you select keyed access mode, you determine the order in which records are read or written by means of character values or integer values called keys. Each READ statement contains the key that locates the record. The key value in the I/O statement is compared with index entries until the record is located.
When you insert a new record, the values contained in the key fields of the record determine the record's placement in the file; you do not have to indicate a key.
You can use keyed access only for indexed files.
Your program can mix keyed access and sequential access I/O statements on the same file. You can use keyed I/O statements to position the file to a particular record, then use sequential I/O statements to access records with either increasing or decreasing key values (depending on the key chosen).
For More Information
On using indexed files, see Chapter 12.
6.8.3 Shared File Use
With the RMS file-sharing capability, you can allow file access by more
than one program at a time or by the same program on more than one
logical unit.
There are two kinds of file sharing:
All three file organizations---relative, indexed, and sequential---permit read and write access to shared files.
The extent to which file sharing can take place is determined by two factors: the type of device on which the file resides and the explicit information supplied by the user. These factors have the following effects:
Depending on the value specified by the ACTION (or READONLY) specifier in the OPEN statement, the file will be opened by your program for reading, writing, or both reading and writing records. This simply checks that the program itself executes the type of statements intended, unless the OPEN statement specifies the SHARED specifier.
To allow other users to access the same file once you have opened it, specify the OPEN statement SHARED specifier when you open the file. If you specify the SHARED specifier when opening a file that is already opened by another process, your program will be allowed to access the file.
If you omit the SHARED specifier when opening a file that is already opened by another process, your program will be denied access to the file. If you omit the SHARED specifier and are the first to open that files, file access might be denied to other users later requesting access to the file.
For performance reasons, when writing records to the file, avoid specifying the SHARED qualifier when you are certain that no other processes will access that file. Similarly, unless you will be writing records when specifying the SHARED qualifier, specify ACTION='READ'.
When two or more programs are write sharing a file, each program should use one of the error-processing mechanisms described in Chapter 14.
Use of one of these controls, the RMS record-locking facility, prevents program failure due to a record-locking error.
The RMS record-locking facility, along with the logic of the program, prevents two processes from accessing the same record at the same time. Record locking ensures that a program can add, delete, or update a record without having to check whether the same record is simultaneously being accessed by another process.
When a program opens a relative, sequential, or indexed file specifying SHARED, RMS locks each record as it is accessed. When a record is locked, any program attempting to access it fails with a record-locked error. A subsequent I/O operation on the logical unit unlocks the previously accessed record, so no more than one record on a logical unit is ever locked.
In the case of a WRITE to a sequential or relative organization file opened for shared access, Compaq Fortran uses an RMS option that causes the record to be updated if it already exists in the file. This option has the side-effect of momentarily releasing the record lock, if any, and then relocking the target record. There is a small possibility that if another program is trying to access the same record at the same time, it may succeed in locking the record while it is unlocked by the first program, resulting in a record-locked error for the WRITE statement.
Locked records can be explicitly unlocked by means of Compaq Fortran's UNLOCK statement. The use of this statement minimizes the amount of time that a record is locked against access by other programs. The UNLOCK statement should be used in programs that retrieve records from a shared file but do not attempt to update them.
For More Information
Previous | Next | Contents | Index |