Previous | Table of Contents | Next |
1.2.20.4. Advancing and Nonadvancing I/O
Advancing input/output is record oriented. Completion of an input/output operation always positions the file at the end of a record. Nonadvancing input/output is character oriented. After reading and writing, the file position may be between characters within a record. Nonadvancing input/output is used by the program char_count, which counts the number of characters in a file:
program char_count integer, parameter :: end_of_record = -2 integer, parameter :: end_of_file = -1 character (len = 1) :: c integer :: character_count, ios character_count = 0 do read (unit = *, fmt = (a), advance = no, iostat = ios) c if (ios == end_of_record) then cycle else if (ios == end_of_file) then exit else character_count = character_count + 1 end if end do print *, The number of characters in the file is, character_count end program char_count
This program also illustrates the use of the iostat specifier to test for end of record and end of file. The values -1 and -2 are common, but not universal.
1.2.20.5. Data Transfer Statements
The data transfer statements are the read, write, and print statements. You have already seen examples of various kinds of data transfer statements. The general forms for the data transfer statements are as follows. Optional parts of a statement appear in square brackets.
read ( io-control-spec-list ) [ input-item-list ] read format [ , input-item-list ] write ( io-control-spec-list ) [ output-item-list ] print format [ , output-item-list ]
Some examples of data transfer statements are
read (unit = 9, iostat = is) x write (unit = 6, rec = 14) y read (f10.2), z print *, zt
The Format Specifier
The format specifier (format in the form for the print statement and the short form of the read statement) may be a character expression indicating explicit formatting or an asterisk (*) indicating list-directed or default formatting.
The Control Information List
The input/output control specification list must contain a unit specifier of the form
unit = io-unit
and may contain at most one each of the following optional items:
fmt = format rec = scalar-integer-expr iostat = scalar-default-integer-variable advance = scalar-character-expr size = scalar-default-integer-variable
The Input/Output List
The input/output list consists basically of variables in a read statement and expressions in a write or print statement.
1.2.20.6. The open Statement
The open statement establishes a connection between a unit and an external file and determines the connection properties. After this is done, the file can be used for data transfers (reading and writing) using the unit number. It is not necessary to execute an open statement for files that are preconnected to a unit. Examples are
open (unit = 9, iostat = ios, status = scratch, & action = readwrite) open (unit = 8, access = direct, file = plot_data, & status = old, action = read)
1.2.20.7. The close Statement
Execution of a close statement terminates the connection of a file to a unit. Any connections not closed explicitly by a close statement are closed by the operating system when the program terminates. Examples are
close (unit = 9) close (unit = 8, iostat = ir, status = keep)
1.2.20.8. The inquire Statement
The inquire statement provides the capability of determining information about a files existence, connection, access method, or other properties during execution of a program. For each property inquired about, a scalar variable of default kind is supplied; that variable is given a value that answers the inquiry. The variable may be tested and optional execution paths selected in a program based on the answer returned. A file inquiry may be made by unit number, by the file name, or by an output list that might be used in an unformatted direct-access output statement. Examples of the inquire statement are
inquire (unit = 9, exist = ex) inquire (file = t123, opened = op, access = ac) inquire (iolength = iolen) x, y, cat
1.2.20.9. File-Positioning Statements
Execution of a data transfer usually changes the position of a file. In addition, there are three statements whose main purpose is to change the position of a file. The backspace statement reverts the position backward by one record. The rewind statement moves the position to the beginning of the file. The endfile statement writes an endfile record and positions the file after the endfile record. Examples of file-positioning statements are
backspace (unit = 8, iostat = status) rewind (unit = 10) endfile (unit = 10, iostat = ierr)
1.2.20.10. Formatting
Data usually is stored in memory as the values of variables in some binary form. For example, the integer 6 may be stored as 0000000000000110, where the 1s and 0s represent bits. On the other hand, formatted data records in a file consist of characters. Thus, when data is read from a formatted record, it must be converted from characters to the internal representation, and when data is written to a formatted record, it must be converted from the internal representation into a string of characters. A format specification provides the information needed to determine how these conversions are to be performed. The format specification is basically a list of edit descriptors, one for each data value in the input/output list of the data transfer statement. The following examples use formatting:
read (unit = *, fmt = (5e10.1, i10)) max_values, k print (a, 2i5), The two values are: , n(1), n(2)
1.2.20.11. List-Directed Formatting
List-directed formatting, also called default formatting, is selected by using an asterisk (*) in place of an explicit format specification in a read, write, or print statement. List-directed editing occurs based on the type of each list item. Examples are
read (unit = 5, fmt = *) a, b, c print *, x(1:n)
Previous | Table of Contents | Next |