| Fortran Language Reference Manual, Volume 2 - S-3693-50 | ||
|---|---|---|
| Prev Section | Next Section | |
Many programs need data to begin a calculation. After the calculation is completed, often the results need to be printed, displayed graphically, or saved for later use. During execution of a program, sometimes there is a large amount of data produced by one part of the program that needs to be saved for use by another part of the program, and the amount of data is too large to store in variables, such as arrays. Also, the editing capabilities of the data transfer statements for internal files can be used for processing character strings. Each of these tasks is accomplished using Fortran I/O statements described in this chapter.
The I/O statements are as follows:
| READ |
| WRITE |
| OPEN |
| CLOSE |
| INQUIRE |
| BACKSPACE |
| ENDFILE |
| REWIND |
The READ statement is a data transfer input statement and provides a means for transferring data from an external media to internal storage or from an internal file to internal storage through a process called reading. The WRITE and PRINT statements are both data transfer output statements and provide a means for transferring data from internal storage to an external media or from internal storage to an internal file. This process is called writing. The OPEN and CLOSE statements are both file connection statements. The INQUIRE statement is a file inquiry statement. The BACKSPACE, ENDFILE, and REWIND statements are file positioning statements.
The first part of this chapter discusses terms and concepts you need to gain a thorough understanding of all of the I/O facilities. These include internal and external files, formatted and unformatted records, sequential and direct access methods for files, advancing and nonadvancing I/O for the sequential formatted access method, file and record positions, units, and file connection properties. Following the concepts are descriptions of the READ, WRITE, and PRINT data transfer statements and the effect of these statements when they are executed. A model for the execution of data transfer statements and a description of the possible error and other conditions created during the execution of data transfer statements are provided next. Following the model are the descriptions of the OPEN, CLOSE, and INQUIRE statements that establish, respectively, the connection properties between units and files; that disconnect units and files; and that permit inquiry about the state of the connection between a unit and file. Lastly, file position statements are specified, which include the BACKSPACE and REWIND statements, followed by the description of the ENDFILE statement that creates end-of-file records.
The chapter concludes with a summary of the terms, concepts, and statements used for input and output processing and some examples.
Collections of data are stored in files. The data in a file is organized into records. Fortran treats a record, for example, as a line on a computer terminal, a line on a listing, or a logical record on a magnetic tape or disk file. However, the general properties of files and records do not depend on how the properties are acquired or how the files and records are stored. This section discusses the properties of records and files, and the various kinds of data transfer.
Note: The Cray Fortran Compiler uses standard UNIX file formats.
A file is a sequence of records. The following section describes the properties of records.
UNICOS/mp physical files and records are described in the Man Page Collection: UNICOS/mp File Formats and Special Files.
There are two kinds of records: data and end-of-file. A data record is a sequence of values.
The values in a data record can be represented in one of two ways: formatted or unformatted. Formatted data consists of characters that are viewable on some medium. For example, a record could contain the following four character values:
| 6 |
| , |
| 1 |
| 1 |
These are intended to represent the two numbers 6 and 11. In this case, the record might be represented schematically as shown in Figure 1-1.
Unformatted data consists of values represented just as they are stored in computer memory. For example, if integers are stored using a binary representation, an unformatted record, consisting of two integer values, 6 and 11, might look like Figure 1-2.
The values in a data record are either all formatted or all unformatted. A formatted record is one that contains only formatted data. It can be created by a person typing at a terminal or by a Fortran program that converts values stored internally into character strings that form readable representations of those values. When a program reads formatted data, the characters must be converted to the computer's internal representation of values. Even character values can be converted from one character representation in the record to another internal representation. The length of a formatted record is the number of characters in it; the length can be zero.
An unformatted record is one that contains only unformatted data. Unformatted records usually are created by running a Fortran program, but they can be created by other means. Unformatted data often requires less space on an external device. Also, it is usually faster to read and write because no conversion is required. However, it is not as suitable for reading by people and usually it is not suitable for transferring data from one computer to another because the internal representation of values is machine-dependent. The length of an unformatted data record depends on the number of values in it, but is measured in bytes; it can be zero. The length of an unformatted record that will be produced by a particular output list can be determined with the INQUIRE statement.
In general, a formatted record is read and written by a formatted data transfer I/O statement, and an unformatted record is read and written by an unformatted data transfer I/O statement.
Another kind of record is the end-of-file record; it has no value and has no length.
Certain file structures allow more than one end-of-file record in a file. For example, the COS blocked format can have multiple end-of-file records. For general information about records, see the Fortran Application Programmer's I/O Reference Manual.
For structures other than COS blocked, an end-of-file record is always the last record of a file. It is used to mark the end of a file. To write it explicitly for files connected for sequential access, use the ENDFILE statement; to write it implicitly, use a file positioning statement (REWIND or BACKSPACE statement), close the file (CLOSE statement), open the file (OPEN statement), or use normal termination of a program.
Note: The Fortran standard does not allow for multiple end-of-file records in a file.
A file can be named. The length of the file name and path name are 256 and 1023 characters, respectively.
A distinction is made between files that are located on an external device like a disk, and files in memory accessible to the program. These two kinds of files are as follows:
External files
Internal files
The use of these files is illustrated schematically in Figure 1-3.
External files are located on external devices such as tapes, disks, or computer terminals. For each external file, there is a set of allowed access methods, a set of allowed forms, a set of allowed actions, and a set of allowed record lengths. These characteristics are determined by a combination of requests by the user of the file and by actions of the operating system. Each characteristic is discussed later in this section.
An external file connected to a unit has the position property. The file is positioned at the current record. In some cases, the file is positioned within the current record.
The contents of internal files are stored as values of variables of type character. You can create the character values by using all the usual means of assigning character values, or you can create them with an output statement that specifies the variable as an internal file. Data transfer to and from internal files is described in Section 1.2.9. Such data transfer to and from an internal file must use formatted sequential access I/O statements, including list-directed data transfer, but not namelist data transfer.
File connection, file positioning, and file inquiry must not be used with internal files. If the variable representing the internal file is a scalar, the file has just one record; if the variable is an array, the file has one record for each element of the array. The order of the records is the order of the elements in the array. The length of each record is the length of one array element.
Certain files are made known to the system for any executing program, and these files are said to exist at the time the program begins executing. A file might not exist because it is not anywhere on the disks accessible to a system or because the user of the program is not authorized to access the file.
In addition to files that are made available to programs for input, output, and other special purposes, programs can create files needed during and after program execution. When the program creates a file, it is said to exist, even if no data has been written into it. A file can cease to exist after it has been deleted. Any of the I/O statements can refer to files that exist for the program at that point during execution. Some of the I/O statements (INQUIRE, OPEN, CLOSE, WRITE, PRINT, REWIND, and ENDFILE) can refer to files that do not exist. A WRITE or PRINT statement can create a file that does not exist and put data into that file, unless an error condition occurs.
An internal file always exists.
Each file being processed by a program has a position. During the course of program execution, records are read or written, causing the file position to change. Also, there are other Fortran statements that cause the file position to change; an example is the BACKSPACE statement. The action produced by the I/O statements is described in terms of the file position, so it is important that file position be discussed in detail.
The initial point is the point just before the first record. The terminal point is the point just after the last record. If the file is empty, the initial point and the terminal point are the same. A file position can become indeterminate, in particular, when an error condition occurs. When the file position becomes indeterminate, the programmer cannot rely on the file being in any particular position.
A file can be positioned between records. In the example pictured in Figure 1-4, the file is positioned between records 2 and 3. In this case, record 2 is the preceding record and record 3 is the next record. Of course, if a file is positioned at its initial point, there is no preceding record, and there is no next record if it is positioned at its terminal point.
There may be a current record during execution of an I/O statement or after completion of a nonadvancing I/O statement as shown in Figure 1-5 where record 2 is the current record. If the file is positioned within a current record, the preceding record is the record immediately previous to the current record, unless the current record is also the initial record, in which case there is no preceding record. Similarly, the next record is the record immediately following the current record, unless the current record is also the final record, in which case there is no next record.
When there is a current record, the file is positioned at the initial point of the record, between values in the record, or at the terminal point of the record.
An internal file is always positioned at the beginning of a record just prior to data transfer.
Advancing I/O is record oriented; completion of such an operation always positions a file at the end of a record or between records, unless an error condition occurs. In contrast, nonadvancing I/O is character oriented; after reading and writing, the file can be positioned between characters within the current record.
While reading a file, the position of a nonadvancing file depends on whether success, data transfer, error, end-of-file, or end-of-record conditions occur while reading the file. The file position is indeterminate following an error condition when reading a file.
While writing a file, the position of a nonadvancing file depends on whether success, data transfer, or error conditions occur while writing the file. The file position is indeterminate following an error condition when writing a file.
When a nonadvancing input operation is performed, the file can be positioned after the last character of the file and before the logical or physical end-of-record. A subsequent nonadvancing input operation causes an end-of-record condition to occur, regardless of whether this record is the last record of the file. If another read operation is executed after the end-of-record condition occurs and the record is the last record of the file, an end-of-file condition occurs.
There are two access file methods:
Sequential access
Direct access
Some files can be accessed by both methods; other files can be restricted to one access method or the other. For example, a magnetic tape can be accessed only sequentially. While each file is connected, it has a set of permissible access methods, which usually means that it can be accessed either sequentially or directly. However, a file cannot be connected for both direct and sequential access simultaneously; that is, if a file is connected for direct access, it must be disconnected with a CLOSE statement and reconnected with an OPEN statement specifying sequential access before it can be referenced in a sequential access data transfer statement, and vice versa.
The actual file access method used to read or write the file is not strictly a property of the file itself, but is indicated when the file is connected to a unit or when the file is created, if the file is preconnected. The same file can be accessed sequentially by a program, then disconnected, and then later accessed directly by the same program, if both types of access are permitted for the file.
Sequential access to the records in the file begins with the first record of the file and proceeds sequentially to the second record, and then to the next record, record-by-record. The records are accessed serially as they appear in the file. It is not possible to begin at some particular record within the file without reading down to that record in sequential order.
When a file is being accessed sequentially, the records are read and written sequentially. For example, if the records are written in any arbitrary order using direct access (see Section 1.1.4.2) and then read using sequential access, the records are read beginning with record number 1 of the file, regardless of when it was written.
An important property of sequential files is that the last record written to the file is always the last record in the file. For example, consider a file with ten records. An application can open the file and read all ten records. If the application subsequently rewinds the file and writes one record, then there is only one record in the file at that time. Each record written to a file truncates any and all records that may have been after it.
When a file is accessed directly, the records are selected by record number. Using this identification, the records can be read or written in any order. Therefore, it is possible to write record number 47 first, then number 13. Reading or writing such records is accomplished by direct access data transfer I/O statements. Either record can be written without first accessing the other.
By default, a formatted file can be created to support sequential or direct access, using the ACCESS specifier on the OPEN statement. A formatted file can support both kinds of access if it is closed and reopened with a different specification and if all records are of equal length. If a file supports both methods of access, the first record accessed sequentially is the record numbered 1 for direct access; the second sequential record is record 2 for direct access, and so on.
While a file is connected for one kind of access (sequential or direct), only I/O statements using that kind of access can be used with the file.
I/O statements refer to a particular file by providing an I/O unit. An I/O unit is either an external unit or an internal unit. An external unit is either a nonnegative integer or an asterisk (*). When an external file is a nonnegative integer, it is called an external file unit. Table 1-1 lists the unit numbers supported by the Cray Fortran Compiler.
A unit number identifies one and only one external unit in all program units in a Fortran program at a time.
File positioning, file connection, and inquiry statements must use an external unit.
An internal unit must be a default character variable. The name of an internal file also is called a unit.
The collection of unit numbers that can be used in a program for external files consists of all non-negative integers. These unit numbers are said to exist. I/O statements must refer to units that exist, except for those that close a file or inquire about a unit.
The assignments for unit numbers 100, 101, and 102 cannot be changed, and these numbers cannot appear in an OPEN statement. A CLOSE statement on these units has no effect.
Numbers 0, 5, and 6 are not equivalent to the asterisk, and they can be reassigned.
The following table shows the unit numbers available:
Table 1-1. Unit Numbers
Unit number | Availability |
|---|---|
0 | Preconnected to stderr but available for user reassignment |
1-4 | Available for user specification |
5 | Preconnected to stdin but available for user reassignment |
6 | Preconnected to stdout but available for user reassignment |
7-99 | Available for user specification |
100 | Dedicated for use as the stdin file |
101 | Dedicated for use as the stdout file |
102 | Dedicated for system as the stderr file |
103-HUGE(3i) | Available for user specification |
In order to transfer data to or from an external file, the file must be connected to a unit. An internal file is always connected to the unit that is the name of the character variable. There are two ways to establish a connection between a unit and an external file:
Execution of an OPEN statement in the executing program
Preconnection by the operating system
Only one file can be connected to a unit at any given time and vice versa. If the unit is disconnected after its first use on a file, it can be reconnected later to another file or to the same file. A file that is not connected to a unit must not be used in any statement, except the OPEN, CLOSE, or INQUIRE statements.
Some units can be preconnected to files for each Fortran program by the operating system without any action necessary by the program. For example, units 0, 5, and 6 are preconnected to files stderr, stdin, and stdout, respectively. All other available units are, in essence, preconnected to the corresponding fort.n file. For example, unit 9 is preconnected to fort.9 and unit 10 is preconnected to fort.10. You also can use the assign(1) command to preconnect units. In either of these cases, the user program does not require an OPEN statement to connect a file for sequential access; it is preconnected.
Once a file has been disconnected, the only way to reference it is by its name, using an OPEN or INQUIRE statement. There is no means of referencing an unnamed file once it is disconnected.
| Prev Section | Table of Contents | Title Page | Index | Next Section |
| Reader Comments | Up one level | Data Transfer Statements |