Previous | Contents | Index |
The maximum record length is 2.147 billion bytes (2,147,483,647 minus the bytes for record overhead). When considering very large record sizes, also consider limiting factors like system virtual memory.
Other file characteristics include:
When you need to display or print formatted data that uses Fortran carriage control, consider using the fpr command as a filter through a pipe to reformat the records into operating system line printer format.
The units used for specifying record length depend on the form of the data:
This section discusses considerations for opening a file (OPEN
statement), obtaining file characteristics and run-time attributes
(INQUIRE statement), and closing a file (CLOSE statement).
7.5.1 Opening Files: The OPEN Statement
To open a file, you should use a preconnected file (such as for
terminal output) or explicitly OPEN files. Although you can also
implicitly open a file, this prevents you from using the OPEN statement
to specify the file connection characteristics and other information.
7.5.1.1 Using Preconnected Standard I/O Files
If you do not use an OPEN statement to open logical unit 5, 6, or 0 and do not set the appropriate environment variable (FORTn), unit number 5 is associated with stdin , unit 6 with stdout , and unit 0 with stderr . At run time, Compaq Fortran implicitly opens (preconnects) units 5, 6, and 0 and associates them with their respective operating system standard I/O files if the corresponding FORTn environment variable is not set.
You can change these preconnected files by using one of the following:
Table 7-4 lists the Compaq Fortran environment variables and the standard I/O file associations for the preconnected files.
Unit | Compaq Fortran Environment Variable | Equivalent Compaq Tru64 UNIX Standard I/O File |
---|---|---|
5 | FORT5 | Standard input, stdin |
6 | FORT6 | Standard output, stdout |
0 | FORT0 | Standard error, stderr |
To change the characteristics of the connection to a preconnected unit, explicitly open the preconnected unit number.
To redirect input or output from the standard preconnected files at run-time, you can set the appropriate Compaq Fortran I/O environment variable (see Section 7.5.1.7) or use the appropriate shell redirection character in a pipe (such as > or <).
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 7.5.2) 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 7-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 or relative). | |
RECORDTYPE 1 indicates which record type to use. | |
FORM indicates whether records are formatted or unformatted. | |
CARRIAGECONTROL 1 indicates the terminal control type. | |
RECL or RECORDSIZE 1 specifies the record size (see Section 7.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 Section 7.7). | |
File Access, Processing, and Position | |
ACCESS indicates the access mode (direct or sequential). | |
SHARED 1 indicates that other users can access the same file and activates record locking. Ignored in the current version of Compaq Fortran for Tru64 UNIX and Linux Systems (see Section 7.6.2). | |
POSITION indicates whether to position the file at the beginning of file, before the end-of-file record, or leave it as is (unchanged). | |
ACTION or READONLY 1 indicates whether statements will be used to only read records, only write records, or both read and write records. | |
MAXREC 1 specifies the maximum record number for direct access. | |
ASSOCIATEVARIABLE 1 specifies the variable containing next record number for direct access. | |
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 10). | |
Error Handling Capabilities | |
ERR specifies a label to branch to if an error occurs. | |
IOSTAT specifies the integer variable to receive the error (IOSTAT) number if an error occurs. | |
File Close Action | |
DISPOSE identifies the action to take when the file is closed. |
You can choose to assign files to logical units by using one of the following methods:
PRINT *,100 |
OPEN (UNIT=7,STATUS='NEW') READ (7,100) |
OPEN (UNIT=7, FILE='FILNAM.DAT', STATUS='OLD') |
The following sections discuss these methods.
7.5.1.4 Accessing Files: Implied and Explicit File and Pathnames
Most I/O operations involve a disk file, keyboard, or screen display. Other devices can also be used:
For information on USEROPEN routines, see Section 7.7.
You can access the terminal screen or keyboard by using preconnected files, as described in Section 7.5.1. Otherwise, this chapter discusses disk files.
A complete file specification consists of a file name usually preceded by a pathname that specifies a directory. The pathname can be in one of two forms:
/usr/users/gdata/testdata |
gdata/testdata |
Directory names and file names should not contain any operating system wildcard characters (such as *, ?, and the [ ] construct). You can use the tilde (~) character as the first character in a pathname to refer to a top-level directory as in the C shell.
When specifying files, keep in mind that trailing and leading blanks are removed from character expression names, but not from Hollerith (numeric array) names.
File names are case sensitive and can consist of uppercase and lowercase letters. For example, the following file names represent three different files:
myfile.for MYfile.for MYFILE.for |
You can associate a logical unit with a directory (if needed) and file
name by using an environment variable assignment (see Section 7.5.1.7)
or by using the OPEN statement (see Section 7.5.1.6). When an OPEN
statement provides a pathname that contains only a directory (no file
name) and an environment variable provides the file name, the OPEN
statement and environment variable can work together to provide the
complete directory and file name.
7.5.1.5 How Compaq Fortran Applies a Default Pathname and File Name
Compaq Fortran provides the following possible ways of specifying all or part of a file specification (directory and file name), such as /usr/proj/testdata :
Compaq Fortran recognizes environment variables for each logical I/O unit number in the form of FORTn, where n is the logical I/O unit number. If a file name is not specified in the OPEN statement and the corresponding FORTn environment variable is not set for that unit number, Compaq Fortran generates a file name in the form fort.n , where n is the logical unit number.
Certain Compaq Fortran environment variables are recognized and preconnected files exist for certain unit numbers, as described in Section 7.5.1.7.
When using scratch files, you can use the TMPDIR environment variable to specify where the scratch file gets created (see Section 7.4.2).
Performing an implied OPEN means that the FILE and DEFAULTFILE specifier values are not specified and an environment variable is used, if present.
Examples of Applying Default Pathnames and File Names
For example, for an implied OPEN of unit number 3, Compaq Fortran would check the environment variable FORT3. If the environment variable FORT3 was set, its value is used. If it is not set, the system supplies the file name fort.3 .
In Table 7-6, assume the current directory is /usr/smith and the I/O uses unit 1, as in the statement READ (1,100) .
OPEN FILE Value |
OPEN DEFAULTFILE Value | FORT1 Environment Variable Value |
Resulting Pathname |
---|---|---|---|
not specified | not specified | not specified | /usr/smith/fort.1 (1) |
not specified | not specified | test.dat | /usr/smith/test.dat (2) |
not specified | not checked | /usr/tmp/t.dat | /usr/tmp/t.dat (3) |
not specified | /tmp | not specified | /tmp/fort.1 (4) |
not specified | /tmp | testdata | /tmp/testdata (5) |
not specified | /usr | lib/testdata | /usr/lib/testdata (6) |
file.dat | /usr/group | not checked | /usr/group/file.dat (7) |
/tmp/file.dat | not checked | not checked | /tmp/file.dat (8) |
file.dat | not specified | not specified | /usr/smith/file.dat (9) |
When the resulting file pathname begins with a tilde character (~), C shell style pathname substitution is used (regardless of what shell is being used), such as a top-level directory (below the root). For additional information on tilde pathname substitution, see csh(1).
Rules for Applying Default Pathnames and File Names
Compaq Fortran determines file name and the directory path based on certain rules. It determines a file name string as follows:
Once Compaq Fortran determines the resulting file name string, it determines the directory (which optionally precedes the file name) as follows:
You can use the FILE and DEFAULTFILE specifiers of the OPEN statement to specify the complete definition of a particular file to be opened on a logical unit. (The Compaq Fortran Language Reference Manual describes the OPEN statement in greater detail.) For example:
OPEN (UNIT=4, FILE='/usr/users/smith/test.dat', STATUS='OLD') |
The file test.dat in directory /usr/users/smith is opened on logical unit 4. No defaults are applied since both the directory and file name were specified. The value of the FILE specifier can be a character constant, variable, or expression.
In the following interactive example, the user supplies the file name and the DEFAULTFILE specifier supplies the default values for the full pathname string. The file to be opened is in /usr/users/smith and is concatenated with the file name typed by the user into the variable DOC:
CHARACTER(LEN=9) DOC WRITE (6,*) 'Type file name ' READ (5,*) DOC OPEN (UNIT=2, FILE=DOC, DEFAULTFILE='/usr/users/smith',STATUS='OLD') |
A slash is appended to the end of the default file string if it does not have one.
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 8-1.
Previous | Next | Contents | Index |