Fortran 77 Language Reference |
Input and Output
This chapter describes the general concepts of FORTRAN input and output, and provides details on the different kinds of I/O. See also the Input/Output chapter in the Fortran Programming Guide.
Essential FORTRAN I/O Concepts
Any operating system based on the UNIX operating system is not as record-oriented as FORTRAN. This operating system treats files as sequences of characters instead of collections of records. The FORTRAN runtime system keeps track of file formats and access mode during runtimes. It also provides the file facilities, including the FORTRAN libraries and the standard I/O library.
Logical Units
The FORTRAN default value for the maximum number of logical units that a program can have open at one time is 64. For current Solaris releases, this limit is 256. A FORTRAN program can increase this limit beyond 64 by calling the
setrlim()
function. See thesetrlim
(2) man page. If you are runningcsh
, you can also do this with thelimit
orunlimit
command; seecsh
(1).The standard logical units 0, 5, and 6 are preconnected as
stderr
,stdin
, andstdout
, respectively. These are not actual file names, and cannot be used for opening these units.INQUIRE
does not return these names, and indicates that the above units are not named unless they have been opened to real files. However, these units can be redefined with anOPEN
statement.The names,
stderr
,stdin
, andstdout
, are meant to make error reporting more meaningful. To preserve error reporting, the system makes it an error to close logical unit 0, although it can be reopened to another file.If you want to open a file with the default file name for any preconnected logical unit, remember to close the unit first. Redefining the standard units can impair normal console I/O. An alternative is to use shell redirection to externally redefine the above units.
To redefine default blank control or the format of the standard input or output files, use the
OPEN
statement, specifying the unit number and no file name, and use the options for the kind of blank control you want.I/O Errors
Any error detected during I/O processing causes the program to abort, unless alternative action has been provided specifically in the program. Any I/O statement can include an
ERR=
clause (andIOSTAT=
clause) to specify an alternative branch to be taken on errors and return the specific error code. Read statements can includeEND=
n to branch on end-of-file. File position and the value of I/O list items are undefined following an error.END=
catches both EOF and error conditions;ERR=
catches only error conditions.If your program does not trap I/O errors, then before aborting, an error message is written to
stderr
with an error number in square brackets,[ ]
, and the logical unit and I/O state. The signal that causes the abort isIOT
.Error numbers less than 1000 refer to operating system errors; see
intro
(2). Error numbers greater than or equal to 1000 come from the I/O library.For external I/O, part of the current record is displayed if the error was caused during reading from a file that can backspace. For internal I/O, part of the string is printed with a vertical bar (
|
) at the current position in the string.General Restriction
Do not reference a function in an I/O list if executing that function causes an I/O statement to be executed. Example:
WRITE( 1, 10) Y, A + 2.0 * F(X)
! Wrong if F() does I/OKinds of I/O
The kinds of I/O supported by
f77
are: formatted, unformatted, binary, list-directed, andNAMELIST
.The two modes of access to files are sequential and direct. When you open a file, the access mode is set to either sequential or direct. If you do not set it explicitly, you get sequential by default.
The two types of files are: external files and internal files. An external file resides on a physical peripheral device, such as disk or tape. An internal file is a location in main memory, is of character type, and is either a variable, substring, array, array element, or a field of a structured record.
Combinations of I/O
I/O combinations on external files are:
The following table shows combinations of I/O form, access mode, and physical file types.
Avoid list-directed internal writes. The number of lines and items per line varies with the values of items.
Printing Files
You get a print file by using the nonstandard
FORM
='PRINT'
inOPEN
.
OPEN ( ..., FORM='PRINT', ... )This specifier works for sequential access files only.
Definition
A print file has the following features:
- With formatted output, you get vertical format control for that logical unit:
- Column one is not printed.
- If column one is blank, 0, or 1, then vertical spacing is one line, two lines, or top of page, respectively.
- If column 1 is
+
, it is replaced by a control sequence that causes a return to the beginning of the previous line.- With list-directed output, you get for that logical unit, column one is not printed.
In general, if you open a file with
FORM
='PRINT'
, then for that file list-directed output does not provide the FORTRAN Standard blank in column one; otherwise, it does provide that blank.FORM
='PRINT'
is for one file per call.If you open a file with
FORM='PRINT'
, then that file has the same content as if it was opened withFORM='FORMATTED'
, and filtered with the output filter,asa
.If you compile with the
-oldldo
option (old list-directed output), then all the files written by the program do list-directed output without that blank in column one; otherwise, they all get that blank. The-oldldo
option is global.The
INQUIRE
StatementThe
INQUIRE
statement returns 'PRINT'
in theFORM
variable for logical units opened as print files. It returns-1
for the unit number of an unopened file.Special Uses of
OPEN
If a logical unit is already open, an
OPEN
statement using theBLANK
option does nothing but redefine that option.As a nonstandard extension, if a logical unit is already open, an
OPEN
statement using theFORM
option to switch betweenFORM='PRINT'
andFORM='FORMATTED'
does nothing but redefine that option.These forms of the
OPEN
statement need not include the file name, and must not include a file name ifUNIT
refers to standard input, output, or standard error.If you connect a unit with
OPEN
and do not use the file name parameter, then you get the default file name,fort.
nn, where nn is the unit number. Therefore, to redefine the standard output as a print file, use:
OPEN( UNIT=6, FORM='PRINT')Scratch Files
Scratch files are temporary files that normally disappears after execution is completed.
Example: Create a scratch file:
OPEN( UNIT=7, STATUS='SCRATCH' )To prevent a temporary file from disappearing after execution is completed, you must execute a
CLOSE
statement withSTATUS='KEEP'
.KEEP
is the default status for all other files.Example: Close a scratch file that you want to access later:
CLOSE( UNIT=7, STATUS='KEEP' )Remember to get the real name of the scratch file. Use
INQUIRE
if you want to reopen it later.Changing I/O Initialization with
IOINIT
Traditional FORTRAN environments usually assume carriage control on all logical units. They usually interpret blank spaces on input as zeroes, and often provide attachment of global file names to logical units at runtime. The routine
IOINIT
(3F) can be called to specify these I/O control parameters. This routine:
- Recognizes carriage control for all formatted files.
- Ignores trailing and embedded blanks in input files.
- Positions files at the beginning or end upon opening.
- Preattaches file names of a specified pattern with logical units.
Example:
IOINIT
and logical unit preattachment:
CALL IOINIT ( .TRUE., .FALSE., .FALSE., 'FORT', .FALSE.)For the above call, the FORTRAN runtime system looks in the environment for names of the form
FORT
nn, and then opens the corresponding logical unit for sequential formatted I/O.With the above example, suppose your program opened unit 7, as follows:
OPEN( UNIT=07, FORM='FORMATTED' )The FORTRAN runtime system looks in the environment for the
FORT07
file, and connects it to unit 7.In general, names must be of the form PREFIXnn, where the particular PREFIX is specified in the call to
IOINIT
, and nn is the logical unit to be opened. Unit numbers less than 10 must include the leading 0. For details, seeIOINIT
(3F) and the Sun Fortran Library Reference.Example: Attach external files
ini1.inp
andini1.out
to units 1 and 2:In
sh
:
demo$TST01=ini1.inp
demo$TST02=ini1.out
demo$export TST01 TST02
In
csh
:
demo%setenv TST01 ini1.inp
demo%setenv TST02 ini1.out
Example: Attach the files,
ini1.inp
andini1.out
, to units 1 and 2:
IOINIT
should prove adequate for most programs as written. However, it is written in FORTRAN so that it can serve as an example for similar user-supplied routines. A copy can be retrieved from:
/opt/SUNWspro/
<release>/src/ioinit.f
The <release> path changes with each release of the compiler.
Direct Access
A direct-access file contains a number of records that are written to or read from by referring to the record number. Direct access is also called random access.
- Records must be all the same length.
- Records are usually all the same type.
- A logical record in a direct access, external file is a string of bytes of a length specified when the file is opened.
- Read and write statements must not specify logical records longer than the original record size definition.
- Shorter logical records are allowed.
- Unformatted direct writes leave the unfilled part of the record undefined.
- Formatted direct writes pass the unfilled record with blanks.
- In using direct unformatted I/O, be careful with the number of values your program expects to read.
- Direct access
READ
andWRITE
statements have an argument,REC=
n, which gives the record number to be read or written. An alternate, nonstandard form is'
n.Unformatted I/O
Example: Direct access, unformatted:
OPEN( 2, FILE='data.db', ACCESS='DIRECT', RECL=20,& FORM='UNFORMATTED', ERR=90 )READ( 2, REC=13, ERR=30 ) X, YREAD( 2 ' 13, ERR=30 ) X, Y ! Alternate formThis code opens a file for direct-access, unformatted I/O, with a record length of 20 characters, then reads the thirteenth record as is.
Formatted I/O
Example: Direct access, formatted:
OPEN( 2, FILE='inven.db', ACCESS='DIRECT', RECL=20,& FORM='FORMATTED', ERR=90 )READ( 2, FMT='(I10,F10.3)', REC=13, ERR=30 ) A, BThis code opens a file for direct-access, formatted I/O, with a record length of 20 characters, then reads the thirteenth record and converts it according to the
(I10,F10.3)
format.Internal Files
An internal file is a character-string object, such as a constant, variable, substring, array, element of an array, or field of a structured record--all of type character. For a variable or substring, there is only a single record in the file but for an array; each array element is a record.
Sequential Formatted I/O
On internal files, the FORTRAN Standard includes only sequential formatted I/O. (I/O is not a precise term to use here, but internal files are dealt with using
READ
andWRITE
statements.) Internal files are used by giving the name of the character object in place of the unit number. The first read from a sequential-access internal file always starts at the beginning of the internal file; similarly for a write.Example: Sequential, formatted reads:
CHARACTER X*80READ( 5, '(A)' ) XREAD( X, '(I3,I4)' ) N1, N2The above code reads a print-line image into
X
, and then reads two integers fromX
.Direct Access I/O
f77
extends direct I/O to internal files.This is like direct I/O on external files, except that the number of records in the file cannot be changed. In this case, a record is a single element of an array of character strings.
Example: Direct access read of the third record of the internal file,
LINE
:
Formatted I/O
- The list items are processed in the order they appear in the list.
- Any list item is completely processed before the next item is started.
- Each sequential access reads or writes one or more logical records.
Input Actions
In general, a formatted read statement does the following:
- Reads character data from the external record or from an internal file.
- Converts the items of the list from character to binary form according to the instructions in the associated format.
- Puts converted data into internal storage for each list item of the list.
Example: Formatted read:
READ( 6, 10 ) A, B10 FORMAT( F8.3, F6.2 )Output Actions
In general, a formatted write statement does the following:
- Gets data from internal storage for each list item specified by the list.
- Converts the items from binary to character form according to the instructions in the associated format.
- Transfers the items to the external record or to an internal file.
- Terminates formatted output records with newline characters.
Example: Formatted write:
REAL A / 1.0 /, B / 9.0 /WRITE( 6, 10 ) A, B10 FORMAT( F8.3, F6.2 )For formatted write statements, the logical record length is determined by the format statement that interacts with the list of input or output variables (I/O list) at execution time.
For formatted write statements, if the external representation of a datum is too large for the field width specified, the specified field is filled with asterisks (
*
).For formatted read statements, if there are fewer items in the list than there are data fields, the extra fields are ignored.
Format Specifiers
Specifiers can be uppercase as well as lowercase characters in format statements and in all the alphabetic arguments to the I/O library routines.
w, m, d, e Parameters (As In
G
w.
dE
e)The definitions for the parameters, w, m, d, and e are:
- w and e are non-zero, unsigned integer constants.
- d and m are unsigned integer constants.
- w specifies that the field occupies w positions.
- m specifies the insertion of leading zeros to a width of m.
- d specifies the number of digits to the right of the decimal point.
- e specifies the width of the exponent field.
Defaults for
w
,d
, ande
You can write field descriptors
A
,D
,E
,F
,G
,I
,L
,O
, orZ
without the w, d, or e field indicators. If these are left unspecified, the appropriate defaults are used based on the data type of the I/O list element. See TABLE 5-3.Typical format field descriptor forms that use w, d, or e include:
A
w, I
w, L
w, O
w,Z
w, D
w.
d, E
w.d, G
w.d, E
w.dE
e, G
w.dE
eExample: With the default w=7 for
INTEGER*2
, and since 161 decimal = A1 hex:
INTEGER*2 MM = 161WRITE ( *, 8 ) M8 FORMAT ( Z )ENDThis example produces the following output:
demo%f77 def1.f
def1.f:MAIN:demo% a.outa1demo%represents a blank character position. The defaults for w, d, and e are summarized in the following table.
For complex items, the value for w is for each real component. The default for the
A
descriptor with character data is the declared length of the corresponding I/O list element.REAL*16
andCOMPLEX*32
are SPARC only.Apostrophe Editing ('aaa')
The apostrophe edit specifier is in the form of a character constant. It causes characters to be written from the enclosed characters of the edit specifier itself, including blanks. An apostrophe edit specifier must not be used on input. The width of the field is the number of characters contained in, but not including, the delimiting apostrophes. Within the field, two consecutive apostrophes with no intervening blanks are counted as a single apostrophe. You can use quotes in a similar way.
Example:
apos.f
, apostrophe edit (two equivalent ways):
WRITE( *, 1 )1 FORMAT( 'This is an apostrophe ''.')WRITE( *, 2 )2 FORMAT( "This is an apostrophe '.")ENDThe above program writes this message twice:
This is an apostrophe '.
Blank Editing (B,BN,BZ)
The
B
,BN
, andBZ
edit specifiers control interpretation of imbedded and trailing blanks for numeric input.The following blank specifiers are available:
BN
--IfBN
precedes a specification, a nonleading blank in the input data is considered null, and is ignored.BZ
--IfBZ
precedes a specification, a nonleading blank in the input data is considered zero.B
--IfB
precedes a specification, it returns interpretation to the default mode of blank interpretation. This is consistent withS
, which returns to the default sign control.Without any specific blank specifiers in the format, nonleading blanks in numeric input fields are normally interpreted as zeros or ignored, depending on the value of the
BLANK=
suboption ofOPEN
currently in effect for the unit. The default value for that suboption isignore
, so if you use defaults for bothBN/BZ/B
andBLANK=
, you getignore
.Example: Read and print the same data once with
BZ
and once withBN
:
Note these rules for blank control:
- Blank control specifiers apply to input only.
- A blank control specifier remains in effect until another blank control specifier is encountered, or format interpretation is complete.
- The
B
,BN
, andBZ
specifiers affect onlyI
,F
,E
,D
, andG
editing.Carriage Control (
$
, space,0
,1
)Use edit descriptor
$
, and space,0
, or1
for carriage control.Dollar $
The special edit descriptor
$
suppresses the carriage return.The action does not depend on the first character of the format. It is used typically for console prompts. For instance, you can use this descriptor to make a typed response follow the output prompt on the same line. This edit descriptor is constrained by the same rules as the colon (
:
).Example: The
$
carriage control:
* dol1.f The $ edit descriptor with spaceWRITE ( *, 2 )2 FORMAT (' Enter the node number: ', $ )READ ( *, * ) NODENUMENDThe above code produces a displayed prompt and user input response, such as:
Enter the node number:82
The first character of the format is printed out, in this case, a blank. For an input statement, the
$
descriptor is ignored.Space, 0, 1, and +
The following first-character slew controls and actions are provided:
TABLE 5-4 Carriage Control with Blank, 0
,1
, and+
(space)
0
1
+One line
Two lines
To first line of next page
No advance (stdout
only, not files)
If the first character of the format is not space,
0
,1
, or +, then it is treated as a space, and it is not printed.The behavior of the slew control character
+
is: if the character in the first column is+
, it is replaced by a control sequence that causes printing to return to the first column of the previous line, where the rest of the input line is printed.Space,
0
,1
, and+
work forstdout
if piped throughasa
.Example: First-character formatting, standard output piped through
asa
:
The program,
slew1.f
produces file,slew1.out
, as printed bylpr
:
bcdefghijklmnop
This starts on a new page. The + of +nop is obeyed.
The results are different on a screen; the tabbing puts in spaces:
demo%
cat slew1.out
bcdefghijnop
This starts on a new page. The + of +nop is obeyed.demo%
The space,
0
, and1
, and+
work for a file opened with:
- Sequential access
FORM='PRINT'
Example: First-character formatting, file output:
The program,
slew2.f
, produces the file,slew2.out
, that is equal to the file,slew1.out
, in the example above.Slew control codes
'0'
,'1'
, and'+'
in column one are in the output file as'\n'
,'\f'
, and'\r'
, respectively.Character Editing (
A
)The
A
specifier is used for character type data items. The general form is:
A[
w]
On input, character data is stored in the corresponding list item.
On output, the corresponding list item is displayed as character data.
- For character data type variables, it assumes the size of the variable.
- For noncharacter data type variables, it assumes the maximum number of characters that fit in a variable of that data type. This is nonstandard behavior.
Each of the following examples read into a size n variable (
CHARACTER*
n), for various values of n, for instance, for n = 9.
CHARACTER C*9READ '( A7 )', CThe various values of n, in
CHARACTER C*
n are:
Data Node
Id
Node
Id
Node
Id
Node
Id
Format A7
A7
A7
A7
Memory Node
Id
Node
Id
e
Id
d
Example: Output strings of 3, 5, and 7 characters, each in a 5 character field:
PRINT 1, 'The', 'whole', 'shebang'1 FORMAT( A5 / A5 / A5 )ENDThe above program displays:
ThewholeshebaThe maximum characters in noncharacter types are summarized in the following table.
In
f77
, you can use Hollerith constants wherever a character constant can be used inFORMAT
statements, assignment statements, andDATA
statements. These constants are not recommended. FORTRAN does not have these old Hollerith (nH
) notations, although the FORTRAN Standard recommends implementing the Hollerith feature to improve compatibility with old programs. But such constants cannot be used as input data elements in list-directed orNAMELIST
input.For example, these two formats are equivalent:
10 FORMAT( 8H Code = , A6 )20 FORMAT( ' Code = ', A6 )In
f77
, commas between edit descriptors are generally optional:
10 FORMAT( 5H flex 4Hible )Reading Into Hollerith Edit Descriptors
For compatibility with older programs,
f77
also allowsREAD
s into Hollerith edit descriptors.Example: Read into hollerith edit descriptor--no list in the
READ
statement:
demo%cat hol1.f
WRITE( *, 1 )1 FORMAT( 6Holder )READ( *, 1 )WRITE( *, 1 )ENDdemo%f77 hol1.f
hol1.f:MAINdemo%a.out
oldernewer
newerdemo%In the above code, if the format is a runtime format (variable format), then the reading into the actual format does not work, and the format remains unchanged. Hence, the following program fails:
CHARACTER F*18 / '(A8)' /READ(*,F) ! Does not work....Obviously, there are better ways to read into the actual format.
Integer Editing (
I
)The
I
specifier is used for decimal integer data items. The general form is:
I[w[.m]]The
I
w andI
w.m edit specifiers indicate that the field to be edited occupies w positions. The specified input/output list item must be of type integer. On input, the specified list item becomes defined with an integer datum. On output, the specified list item must be defined as an integer datum.On input, an
I
w.m edit specifier is treated identically to anI
w edit specifier.The output field for the
I
w edit specifier consists of:
- Zero or more leading blanks followed by
- Either a minus if the value is negative, or an optional plus, followed by
- The magnitude of the value in the form on an unsigned integer constant without leading zeros
An integer constant always has at least one digit.
The output field for the
I
w.m edit specifier is the same as for theI
w edit specifier, except that the unsigned integer constant consists of at least m digits, and, if necessary, has leading zeros. The value of m must not exceed the value of w. If m is zero, and the value of the item is zero, the output field consists of only blank characters, regardless of the sign control in effect.Example:
int1.f
, integer input:
CHARACTER LINE*8 / '12345678' /READ( LINE, '(I2, I3, I2 )') I, J, KPRINT *, I, J, KENDThe program above displays:
12 345 67Example:
int2.f
, integer output:
N = 1234PRINT 1, N, N, N, N1 FORMAT( I6 / I4 / I2 / I6.5 )ENDThe above program displays:
12341234**01234Logical Editing (
L
)The
L
specifier is used for logical data items. The general form is:
LwThe
L
w edit specifier indicates that the field occupies w positions. The specified input/output list item must be of typeLOGICAL
. On input, the list item becomes defined with a logical datum. On output, the specified list item must be defined as a logical datum.The input field consists of optional blanks, optionally followed by a decimal point, followed by a
T
for true, orF
for false. TheT
orF
can be followed by additional characters in the field. The logical constants,.TRUE.
and.FALSE.,
are acceptable as input. The output field consists of w-1 blanks followed by aT
for true, orF
for false.Example:
log1.f
, logical output:
LOGICAL A*1 /.TRUE./, B*2 /.TRUE./, C*4 /.FALSE./PRINT '( L1 / L2 / L4 )', A, B, CENDThe program above displays:
TTFExample:
log2.f
, logical input:
LOGICAL*4 A1 READ '(L8)', APRINT *, AGO TO 1ENDThe program above accepts any of the following as valid input data:
t true T TRUE .t .t. .T .T. .TRUE. TooTruef false F FALSE .f .F .F. .FALSE. FlakeyOctal and Hexadecimal Editing (
O
,Z
)The
O
andZ
field descriptors for aFORMAT
statement are for octal and hexadecimal integers, respectively, but they can be used with any data type.
where w is the number of characters in the external field. For output, m, if specified, determines the total number of digits in the external field; that is, if there are fewer than m nonzero digits, the field is zero-filled on the left to a total of m digits. m has no effect on input.
Octal and Hex Input
A
READ
, with theO
orZ
field descriptors in theFORMAT
, reads in w characters as octal or hexadecimal, respectively, and assigns the value to the corresponding member of the I/O list.Example: Octal input, the external data field is:
654321The first digit in the example appears in input column 1.
The program that does the input is:
READ ( *, 2 ) M2 FORMAT ( O6 )The above data and program result in the octal value 654321 being loaded into the variable
M
. Further examples are included in the following table.
TABLE 5-6 Sample Octal/Hex Input Values O4
O4
O31234
16234
971234
1623
Error: "9" not allowedZ5
Z5
Z4A23DE
A23DEF
95.AF2A23DE
A23DEError: "." not allowed
The general rules for octal and hex input are:
- For octal values, the external field can contain only numerals 0 through 7.
- For hexadecimal values, the external field can contain only numerals 0 through 9 and the letters
A
throughF
ora
throughf
.- Signs, decimal points, and exponent fields are not allowed.
- All-blank fields are treated as having a value of zero.
- If a data item is too big for the corresponding variable, an error message is displayed.
Octal and Hex Output
A
WRITE
, with theO
orZ
field descriptors in theFORMAT
, writes out values as octal or hexadecimal integers, respectively. It writes to a field that is w characters wide, right-justified.Example: Hex output:
M = 161WRITE ( *, 8 ) M8 FORMAT ( Z3 )ENDThe program above displays
A1
(161 decimal = A1 hex):
A1The letter
A
appears in output column 2.Further examples are included in the following table.
TABLE 5-7 Sample Octal/Hex Output Value O6
O2
O4.3
O4.4
O632767
14251
27
27
-3276777777
**
033
0033
*****Z4
Z3.3
Z6.4
Z532767
2708
2708
-327677FFF
A94
0A94
******
The general rules for octal and hex output are:
- Negative values are written as if unsigned; no negative sign is printed.
- The external field is filled with leading spaces, as needed, up to the width w.
- If the field is too narrow, it is filled with asterisks.
- If m is specified, the field is left-filled with leading zeros, to a width of m.
Positional Editing
For horizontal positioning along the print line,
f77
supports the forms:where n is a strictly positive integer. The format specifier
T
can appear by itself, or be preceded or followed by a positive nonzero number.
T
n--Absolute ColumnsThis tab reads from the nth column or writes to the nth column. If n is missing it is treated as T1.
TLn--Relative Columns
This tab reads from the nth column to the left or writes to the nth column to the left. If n is missing it is treated as TL0 .
TRn--Relative Columns
This tab reads from the nth column to the right or writes to the nth column to the right. If n is missing it is treated as TR0 .
nT--Relative Tab Stop
This tab tabs to the next tab stop for both read and write. If n is omitted, this tab uses n = 1 and tabs to the next tab stop. (This edit specifier is not standard FORTRAN 77)
The rules and Restrictions for tabbing are:
- Tabbing right beyond the end of an input logical record is an error.
- Tabbing left beyond the beginning of an input logical record leaves the input pointer at the beginning of the record.
- Nondestructive tabbing is implemented for both internal and external formatted I/O. Nondestructive tabbing means that tabbing left or right on output does not destroy previously written portions of a record.
- Tabbing right on output causes unwritten portions of a record to be filled with blanks.
- Tabbing left requires that the logical unit allows a
seek
. Therefore, it is not allowed in I/O to or from a terminal or pipe.- Likewise, nondestructive tabbing in either direction is possible only on a unit that can seek. Otherwise, tabbing right or spacing with the
X
edit specifier writes blanks on the output.- Tab stops are hard-coded every eight columns.
nX--Positions
The n
X
edit specifier indicates that the transmission of the next character to or from a record is to occur at the position n characters forward from the current position.On input, the n
X
edit specifier advances the record pointer by n positions, skipping n characters.A position beyond the last character of the record can be specified if no characters are transmitted from such positions.
On output, the n
X
specifier writes n blanks.Example: Input,
T
n (absolute tabs):
demo%cat rtab.f
CHARACTER C*2, S*2OPEN( 1, FILE='mytab.data')DO I = 1, 2READ( 1, 2 ) C, S2 FORMAT( T5, A2, T1, A2 )PRINT *, C, SEND DOENDdemo%The two-line data file is:
demo%cat mytab.data
defguvwx12345678demo%The run and the output are:
demo%a.out
uvde5612demo%The above example first reads columns 5 and 6, then columns 1 and 2.
Example: Output
T
n (absolute tabs); this program writes an output file:
demo%cat otab.f
CHARACTER C*20 / "12345678901234567890" /OPEN( 1, FILE='mytab.rep')WRITE( 1, 2 ) C, ":", ":"2 FORMAT( A20, T10, A1, T20, A1 )ENDdemo%The output file is:
demo%cat mytab.rep
123456789:123456789:demo%The above example writes 20 characters, then changes columns 10 and 20.
Example: Input,
TR
n andTL
n (relative tabs)--the program reads:
demo%cat rtabi.f
CHARACTER C, S, TOPEN( 1, FILE='mytab.data')DO I = 1, 2READ( 1, 2 ) C, S, T2 FORMAT( A1, TR5, A1, TL4, A1 )PRINT *, C, S, TEND DOENDdemo%The two-line data file is:
demo%cat mytab.data
defguvwx12345678demo%The run and the output are:
demo%a.out
dwg174demo%The above example reads column 1, then tabs right 5 to column 7, then tabs left 4 to column 4.
Example: Output
TR
n andTL
n (relative tabs)--this program writes an output file:
demo%cat rtabo.f
CHARACTER C*20 / "12345678901234567890" /OPEN( 1, FILE='rtabo.rep')WRITE( 1, 2 ) C, ":", ":"2 FORMAT( A20, TL11, A1, TR9, A1 )ENDdemo%The run shows nothing, but you can list the
mytab.rep
output file:
demo%cat rtabo.rep
123456789:123456789:demo%The above program writes 20 characters, tabs left 11 to column 10, then tabs right 9 to column 20.
Quotes Editing (
"aaa"
)The quotes edit specifier is in the form of a character constant.It causes characters to be written from the enclosed characters of the edit specifier itself, including blanks. A quotes edit specifier must not be used on input.
The width of the field is the number of characters contained in, but not including, the delimiting quotes. Within the field, two consecutive quotes with no intervening blanks are counted as a single quote. You can use apostrophes in a similar way.
Example:
quote.f
(
two equivalent ways):
WRITE( *, 1 )1 FORMAT( 'This is a quote ".' )WRITE( *, 2 )2 FORMAT( "This is a quote ""." )ENDThis program writes this message twice: This is a quote ".
Radix Control (
R
)The format specifier is
R
or nR
, where 2 n 36. If n is omitted, the default decimal radix is restored.You can specify radixes other than 10 for formatted integer I/O conversion. The specifier is patterned after
P
, the scale factor for floating-point conversion. It remains in effect until another radix is specified or format interpretation is complete. The I/O item is treated as a 32-bit integer.Example: Radix 16--the format for an unsigned, hex, integer, 10 places wide, zero-filled to 8 digits, is
(su, 16r, I10.8)
, as in:
demo%cat radix.f
integer i / 110 /write( *, 1 ) i1 format( SU, 16r, I10.8 )enddemo%f77 -silent radix.f
demo%a.out
0000006Edemo%
SU
is described in Sign Editing (SU, SP, SS, S).Editing
REAL
Data (D
,E
,F
,G
)The
D
,E
,F
, andG
specifiers are for decimal real data items.D Editing
The D specifier is for the exponential form of decimal double-precision items. The general form is
:
D[
w[.
d]]
The
D
w andD
w.d edit specifiers indicate that the field to be edited occupies w positions. d indicates that the fractional part of the number (the part to the right of the decimal point) has d digits. However, if the input datum contains a decimal point, that decimal point overrides the d value.On input, the specified list item becomes defined with a real datum. On output, the specified list item must be defined as a real datum.
In an output statement, the
D
edit descriptor does the same thing as theE
edit descriptor, except that aD
is used in place of anE
. The output field for theD
w.d edit specifier has the width w. The value is right-justified in that field. The field consists of zero or more leading blanks followed by either a minus if the value is negative, or an optional plus, followed by the magnitude of the value of the list item rounded to d decimal digits.w must allow for a minus sign, at least one digit to the left of the decimal point, the decimal point, and d digits to the right of the decimal point. Therefore, it must be the case that w d+3.
Example: Real input with
D
editing in the program,Dinp.f
:
CHARACTER LINE*24 / '12345678 23.5678 .345678' /READ( LINE, '( D8.3, D8.3, D8.3 )') R, S, TPRINT '( D10.3, D11.4, D13.6 )', R, S, TENDThe above program displays:
0.123D+05 0.2357D+02 0.345678D+00In the above example, the first input data item has no decimal point, so
D8.3
determines the decimal point. The other input data items have decimal points, so those decimal points override theD
edit descriptor as far as decimal points are concerned.Example: Real output with
D
editing in the programDout.f
:
R = 1234.678PRINT 1, R, R, R1 FORMAT( D9.3 / D8.4 / D13.4 )ENDThe above program displays:
0.123D+04********0.1235D+04In the above example, the second printed line is asterisks because the
D8.4
does not allow for the sign; in the third printed line theD13.4
results in three leading blanks.E Editing
The
E
specifier is for the exponential form of decimal real data items. The general form is:
E[
w[.
d][E
e]]
w indicates that the field to be edited occupies w positions.
d indicates that the fractional part of the number (the part to the right of the decimal point) has d digits. However, if the input datum contains a decimal point, that decimal point overrides the d value.
e indicates the number of digits in the exponent field. The default is 2.
The specified input/output list item must be of type real. On input, the specified list item becomes defined with a real datum. On output, the specified list item must be defined as a real datum.
The output field for the
E
w.d edit specifier has the width w. The value is right-justified in that field. The field consists of zero or more leading blanks followed by either a minus if the value is negative, or an optional plus, followed by a zero, a decimal point, the magnitude of the value of the list item rounded to d decimal digits, and an exponent.
- If | exponent | 99, it has the form
E±
nn or0±
nn.- If 99 | exponent | 999, it has the form
±
nnn.For the form
E
w.dE
e, if | exponent |( 10e ) - 1, then the exponent has the form
±
nnn.
- If | exponent | 99, it has the form
D±
nn orE±
nn or0±
nn.- If 99 | exponent | 999, it has the form
±
nnn.The sign in the exponent is required.
w need not allow for a minus sign, but must allow for a zero, the decimal point, and d digits to the right of the decimal point, and an exponent. Therefore, for nonnegative numbers, w d+6; if e is present, then w d+e+4. For negative numbers, w d+7; if e is present, then w d+e+5.
Example: Real input with
E
editing in the program,Einp.f
:
CHARACTER L*40/'1234567E2 1234.67E-3 12.4567 '/READ( L, '( E9.3, E12.3, E12.6 )') R, S, TPRINT '( E15.6, E15.6, E15.7 )', R, S, TENDThe above program displays:
0.123457E+060.123467E+010.1245670E+02In the above example, the first input data item has no decimal point, so
E9.3
determines the decimal point. The other input data items have decimal points, so those decimal points override theD
edit descriptor as far as decimal points are concerned.Example: Real output with
E
editing in the programEout.f
:
R = 1234.678PRINT 1, R, R, R1 FORMAT( E9.3 / E8.4 / E13.4 )ENDThe above program displays:
0.123E+04********0.1235E+04In the above example,
E8.4
does not allow for the sign, so we get asterisks. Also, the extra wide field of theE13.4
results in three leading blanks.Example: Real output with
E
w.dE
e editing in the programEwdEe.f
:
REAL X / 0.000789 /WRITE(*,'( E13.3)') XWRITE(*,'( E13.3E4)') XWRITE(*,'( E13.3E5)') XENDThe above program displays:
0.789E-030.789E-00030.789E-00003F Editing
The F specifier is for decimal real data items. The general form is
:
F[
w[.
d]]
The
F
w andF
w.d edit specifiers indicate that the field to be edited occupies w positions.d indicates that the fractional part of the number (the part to the right of the decimal point) has d digits. However, if the input datum contains a decimal point, that decimal point overrides the d value.
The specified input/output list item must be of type real. On input, the specified list item becomes defined with a real datum. On output, the specified list item must be defined as a real datum.
The output field for the
F
w.d edit specifier has the width w. The value is right-justified in that field. The field consists of zero or more leading blanks followed by either a minus if the value is negative, or an optional plus, followed by the magnitude of the value of the list item rounded to d decimal digits.w must allow for a minus sign, at least one digit to the left of the decimal point, the decimal point, and d digits to the right of the decimal point. Therefore, it must be the case that w d+3.
Example: Real input with
F
editing in the programFinp.f
:
CHARACTER LINE*24 / '12345678 23.5678 .345678' /READ( LINE, '( F8.3, F8.3, F8.3 )') R, S, TPRINT '( F9.3, F9.4, F9.6 )', R, S, TENDThe program displays:
12345.678DD23.5678D0.345678In the above example, the first input data item has no decimal point, so
F8.3
determines the decimal point. The other input data items have decimal points, so those decimal points override theF
edit descriptor as far as decimal points are concerned.Example: Real output with
F
editing in the programFout.f
:
R = 1234.678PRINT 1, R, R, R1 FORMAT( F9.3 / F8.4 / F13.4 )ENDThe above program displays:
1234.678********1234.6780In the above example,
F8.4
does not allow for the sign;F13.4
results in four leading blanks and one trailing zero.G Editing
The
:G
specifier is for decimal real data items. The general forms are
G[
w[.
d]]
orG
w.dE
eThe
D
,E
,F
, andG
edit specifiers interpret data in the same way.The representation for output by the
G
edit descriptor depends on the magnitude of the internal datum. In the following table, N is the magnitude of the internal datum.
0.1
N < 1.01.0
N < 10.0
...
10
(d-2) N < 10(d-1)
10
(d-1) N < 10dF
(w-4
).dF
(w-4
).(d-1)
...F
(w-4
).1F
(w-4
).0
Commas in Formatted Input
If you are entering numeric data that is controlled by a fixed-column format, then you can use commas to override any exacting column restrictions.
Example: Format:
(I10, F20.10, I4)Using the above format reads the following record correctly:
-345,.05e-3,12The I/O system is just being more lenient than described in the FORTRAN Standard. In general, when doing a formatted read of noncharacter variables, commas override field lengths. More precisely, for the
I
w,F
w.d,E
w.d[Ee
], andG
w.d input fields, the field ends when w characters have been scanned, or a comma has been scanned, whichever occurs first. If it is a comma, the field consists of the characters up to, but not including, the comma; the next field begins with the character following the comma.Remaining Characters (
Q
)The
Q
edit descriptor gets the length of an input record or the remaining portion of it that is unread. It gets the number of characters remaining to be read from the current record.Example: From a real and a string, get: real, string length, and string:
The above program reads a field into the variable
R
, then reads the number of characters remaining after that field intoL
, then readsL
characters intoCVECT
.Q
as the nth edit descriptor matches withL
as the nth element in theREAD
list.Example: Get length of input record; put the
Q
descriptor first:
The above example gets the length of the input record. With the whole input string and its length, you can then parse it yourself.
Several restrictions on the
Q
edit descriptor apply:
- The list element
Q
corresponds to must be ofINTEGER
orLOGICAL
data type.Q
does strictly a character count. It gets the number of characters remaining in the input record, and does not get the number of integers or reals or anything else.- The
Q
edit descriptor cannot be applied for pipe files, asQ
edit requires that the file be rereadable.- This descriptor operates on files and stdin (terminal) input.
- This descriptor is prohibited for output and will generate a runtime error.
Scale Factor (
P
)The
P
edit descriptor scales real input values by a power of 10. It also gives you more control over the significant digit displayed for output values.[k]
P
Parameter Description k Integer constant, with an optional sign
k is called the scale factor, and the default value is zero.
Example: I/O statements with scale factors:
READ ( 1, '( 3P E8.2 )' ) XWRITE ( 1, '( 1P E8.2 )' ) X
P
by itself is equivalent to0P
. It resets the scale factor to the default value0P
. JustP
by itself is nonstandard.Scope
The scale factor is reset to zero at the start of execution of each I/O statement. The scale factor can have an effect on
D
,E
,F
, andG
edit descriptors.Input
On input, any external datum that does not have an exponent field is divided by 10k before it is stored internally.
Input examples: Showing data, scale factors, and resulting value stored:
Data 18.63
18.63
18.63E2
18.63
Format E8.2
3P E8.2
3P E8.2
-3P E8.2
Memory 18.63
.01863
18.63E2
18630.
Output
On output, with
D
, andE
descriptors, and withG
descriptors if theE
editing is required, the internal item gets its basic real constant part multiplied by 10k, and the exponent is reduced by k before it is written out.On output with the
F
descriptor and withG
descriptors, if theF
editing is sufficient, the internal item gets its basic real constant part multiplied by 10k before it is written out.Output Examples: Showing value stored, scale factors, and resulting output:
Memory 290.0
290.0
290.0
290.0
Format 2P E9.3
1P E9.3
-1P E9.3
F9.3
Display 29.00E+01
2.900E+02
0.029E+04
0.290E+03
Sign Editing (
SU
,SP
,SS
,S
)The
SU
,SP
, andS
edit descriptors control leading signs for output. For normal output, without any specific sign specifiers, if a value is negative, a minus sign is printed in the first position to the left of the leftmost digit; if the value is positive, printing a plus sign depends on the implementation, butf77
omits the plus sign.The following sign specifiers are available:
SP
--IfSP
precedes a specification, a sign is printed.SS
--IfSS
precedes a specification, plus-sign printing is suppressed.S
--IfS
precedes a specification, the system default is restored. The default isSS
.SU
--IfSU
precedes a specification, integer values are interpreted as unsigned. This is nonstandard.For example, the unsigned specifier can be used with the radix specifier to format a hexadecimal dump, as follows:
2000 FORMAT( SU, 16R, 8I10.8 )The rules and restrictions for sign control are:
- Sign-control specifiers apply to output only.
- A sign-control specifier remains in effect until another sign-control specifier is encountered, or format interpretation is complete.
- The
S
,SP
, andSS
specifiers affect onlyI
,F
,E
,D
, andG
editing.- The
SU
specifier affects onlyI
editing.Slash Editing (
/
)The slash (
/
) edit specifier indicates the end of data transfer on the current record.Sequential Access
On input, any remaining portion of the current record is skipped, and the file is positioned at the beginning of the next record. Two successive slashes (
//
) skip a whole record.On output, an end-of-record is written, and a new record is started. Two successive slashes (
//
) produce a record of no characters. If the file is an internal file, that record is filled with blanks.Direct Access
Each slash increases the record number by one, and the file is positioned at the start of the record with that record number.
On output, two successive slashes (
//
) produce a record of no characters, and that record is filled with blanks.Termination Control (
:
)The colon (:) edit descriptor allows for conditional termination of the format. If the I/O list is exhausted before the format, then the format terminates at the colon.
Example: Termination control:
* col1.f The colon (:) edit descriptorDATA INIT / 3 /, LAST / 8 /WRITE ( *, 2 ) INITWRITE ( *, 2 ) INIT, LAST2 FORMAT ( 1X 'INIT = ', I2, :, 3X, 'LAST = ', I2 )ENDThe above program produces output like the following
INIT = 3INIT = 3 LAST = 8Without the colon, the output is more like this:
INIT = 3 LAST =INIT = 3 LAST = 8Runtime Formats
You can put the format specifier into an object that you can change during execution. Doing so improves flexibility. There is some increase in execution time because this kind of format specifier is parsed every time the I/O statement is executed. These are also called variable formats.
The object must be one of the following kinds:
- Character expression--The character expression can be a scalar, an array, an element of an array, a substring, a field of a structured record , the concatenation of any of the above, and so forth.
- Integer array --The integer array can get its character values by a
DATA
statement, an assignment statement, aREAD
statement, and so forth.You must provide the delimiting left and right parentheses, but not the word
FORMAT
, and not a statement number.You must declare the object so that it is big enough to hold the entire format. For instance,
'(8X,12I)'
does not fit in anINTEGER*4
or aCHARACTER*4
object.Examples: Runtime formats in character expressions and integer arrays
:
Variable Format Expressions (
<
e>
)In general, inside a
FORMAT
statement, any integer constant can be replaced by an arbitrary expression.The expression itself must be enclosed in angle brackets.
For example, the
6
in:
1 FORMAT( 3F6.1 )can be replaced by the variable
N
, as in:
1 FORMAT( 3F<N>.1 )or by the slightly more complicated expression
2*N+M
, as in:
1 FORMAT( 3F<2*N+M>.1 )Similarly, the
3
or1
can be replaced by any expression.The single exception is the n in an n
H
... edit descriptor.The rules and restrictions for variable format expressions are:
- The expression is reevaluated each time it is encountered in a format scan.
- If necessary, the expression is converted to integer type.
- Any valid FORTRAN expression is allowed, including function calls.
- Variable expressions are not allowed in formats generated at runtime.
- The n in an n
H
... edit descriptor cannot be a variable expression.Unformatted I/O
Unformatted I/O is used to transfer binary information to or from memory locations without changing its internal representation. Each execution of an unformatted I/O statement causes a single logical record to be read or written. Since internal representation varies with different architectures, unformatted I/O is limited in its portability.
You can use unformatted I/O to write data out temporarily, or to write data out quickly for subsequent input to another FORTRAN program running on a machine with the same architecture.
Sequential Access I/O
Logical record length for unformatted, sequential files is determined by the number of bytes required by the items in the I/O list. The requirements of this form of I/O cause the external physical record size to be somewhat larger than the logical record size.
The FORTRAN runtime system embeds the record boundaries in the data by inserting an
INTEGER*4
byte count at the beginning and end of each unformatted sequential record during an unformatted sequentialWRITE
. The trailing byte count enablesBACKSPACE
to operate on records. The result is that FORTRAN programs can use an unformatted sequentialREAD
only on data that was written by an unformatted sequentialWRITE
operation. Any attempt to read such a record as formatted would have unpredictable results.
- Avoid using the unformatted sequential
READ
unless your file was written that way.- Because of the extra data at the beginning and end of each unformatted sequential record, you might want to try using the unformatted direct
I/O
whenever that extra data is significant. It is more significant with short records than with very long ones.Direct Access I/O
If your I/O lists are different lengths, you can
OPEN
the file with theRECL=1
option. This signals FORTRAN to use the I/O list to determine how many items to read or write.For each read, you still must tell it the initial record to start at, in this case which byte, so you must know the size of each item.
Example: Direct access--create 3 records with 2 integers each:
Example: Direct access--read the 3 records:
Here we knew beforehand the size of the records on the file. In this case we can read the file just as it was written.
However, if we only know the size of each item but not the size of the records on a file we can use
recl=1
on theOPEN
statement to have the I/O list itself determine how many items to read:Example: Direct-access read, variable-length records,
recl=1
:
In the above example, after reading 3 integers (12 bytes), you start the next read at record 13.
Binary I/O
Opening a file for binary I/O allows the program to read and write data as a stream of binary data without record boundaries. (This feature is not standard Fortran 77).
The
FORM='BINARY'
option on anOPEN
statement declares that unit to be a sequential unformatted file without record marks:
OPEN(1, FORM='BINARY')A binary file cannot also be declared direct-access or formatted.
On a
WRITE
statement, binary data is written to the file as a stream of bytes, as many as there are in the output list. On aREAD
statement, as many data bytes are read as demanded by the variables on the input list. Since there are no record marks (end-of-record) in the file, there will be no possibility of reading past a record mark. Other than abnormal system errors, the only situation that the program can detect is reading in the end-of-file. EachREAD
statement just reads the next sequence of bytes in the file, as shown in the following example:
An
INQUIRE
on a binary file returns`BINARY'
for theFORM=
parameter,`SEQUENTIAL'
forACCESS=
,`YES'
forUNFORMATTED=
,`YES'
forSEQUENTIAL
.
BACKSPACE
on a binary file is not allowed and causes a runtime error message.List-Directed I/O
List-directed I/O is a free-form I/O for sequential access devices. To get it, use an asterisk as the format identifier, as in:
READ( 6, * ) A, B, CNote these rules for list-directed input:
- On input, values are separated by strings of blanks and, possibly, a comma.
- Values, except for character strings, cannot contain blanks.
- Character strings can be quoted strings, using pairs of quotes (
"
), pairs of apostrophes ('), or unquoted strings (see Unquoted Strings), but not hollerith (nHxyz
) strings.- End-of-record counts as a blank, except in character strings, where it is ignored.
- Complex constants are given as two real constants separated by a comma and enclosed in parentheses.
- A null input field, such as between two consecutive commas, means that the corresponding variable in the I/O list is not changed.
- Input data items can be preceded by repetition counts, as in:
4*(3.,2.) 2*, 4*'hello'
- The above input stands for 4 complex constants, 2 null input fields, and 4 string constants.
- A slash (
/
) in the input list terminates assignment of values to the input list during list-directed input, and the remainder of the current input line is skipped. Any text that follows the slash is ignored and can be used to comment the data line.Output Format
List-directed output provides a quick and easy way to print output without fussing with format details. If you need exact formats, use formatted I/O. A suitable format is chosen for each item, and where a conflict exists between complete accuracy and simple output form, the simple form is chosen.
Note these rules for list-directed output:
- In general, each record starts with a blank space. For a print file, that blank is not printed. See Printing Files for details.
- Character strings are printed as is. They are not enclosed in quotes, so only certain forms of strings can be read back using list-directed input. These forms are described in the next section.
- A number with no exact binary representation is rounded off.
- Example: No exact binary representation:
demo%cat lis5.f
READ ( 5, * ) XWRITE( 6, * ) X, ' beauty'WRITE( 6, 1 ) X1 FORMAT( 1X, F13.8, ' truth' )ENDdemo%f77 lis5.f
lis5.f:MAIN:demo%a.out
1.4
1.40000000 beauty1.39999998 truthdemo%- In the above example, if you need accuracy, specify the format.
- Output lines longer than 80 characters are avoided where possible.
- Complex and double complex values include an appropriate comma.
- Real, double, and quadruple precision values are formatted differently.
- A backslash-
n
(\n
) in a character string is output as a carriage return, unless the -xl
option is on, and then it is output as a backslash-n
(\n
).Example: List-directed I/O and backslash-n, with and without
-xl
:
demo%cat f77 bslash.f
CHARACTER S*8 / '12\n3' /PRINT *, SENDdemo%Without
-xl
,\n
prints as a carriage return:
demo%f77 -silent bslash.f
demo%a.out
123demo%With
-xl
,\n
prints as a character string:
demo%f77 -xl -silent bslash.f
demo%a.out
12\n3demo%
COMPLEX*32
andREAL*16
are SPARC only.Unquoted Strings
f77
list-directed I/O allows reading of a string not enclosed in quotes.The string must not start with a digit, and cannot contain separators (commas or slashes (
/
)) or whitespace (spaces or tabs). A newline terminates the string unless escaped with a backslash (\
). Any string not meeting the above restrictions must be enclosed in single or double quotes.Example: List-directed input of unquoted strings:
CHARACTER C*6, S*8READ *, I, C, N, SPRINT *, I, C, N, SENDThe above program,
unquoted.f
, reads and displays as follows:
demo%a.out
23 label 82 locked
23label 82lockeddemo%Internal I/O
f77
extends list-directed I/O to allow internal I/O.During internal, list-directed reads, characters are consumed until the input list is satisfied or the end-of-file is reached. During internal, list-directed writes, records are filled until the output list is satisfied. The length of an internal array element should be at least 20 characters to avoid logical record overflow when writing double-precision values. Internal, list-directed read was implemented to make command line decoding easier. Internal, list-directed output should be avoided.
NAMELIST
I/O
NAMELIST
I/O produces format-free input or output of whole groups of variables, or input of selected items in a group of variables.The
NAMELIST
statement defines a group of variables or arrays. It specifies a group name, and lists the variables and arrays of that group.Syntax Rules
The syntax of the
NAMELIST
statement is:
NAMELIST /
group-name/
namelist[[,]/
group-name/namelist]
...
Parameter Description group-name Name of group namelist List of variables or arrays, separated by commas
See NAMELIST for details.
Example:
NAMELIST
statement:
CHARACTER*18 SAMPLELOGICAL*4 NEWREAL*4 DELTANAMELIST /CASE/ SAMPLE, NEW, DELTAA variable or array can be listed in more than one
NAMELIST
group.The input data can include array elements and strings. It can include substrings in the sense that the input constant data string can be shorter than the declared size of the variable.
Restrictions
group name can appear in only the
NAMELIST
,READ
, orWRITE
statements, and must be unique for the program.list cannot include constants, array elements, dummy assumed-size arrays, structures, substrings, records, record fields, pointers, or pointer-based variables.
Example: A variable in two
NAMELIST
groups:
REAL ARRAY(4,4)CHARACTER*18 SAMPLELOGICAL*4 NEWREAL*4 DELTANAMELIST /CASE/ SAMPLE, NEW, DELTANAMELIST /GRID/ ARRAY, DELTAIn the above example,
DELTA
is in the groupCASE
and in the groupGRID
.Output Actions
NAMELIST
output uses a special form ofWRITE
statement, which makes a report that shows the group name. For each variable of the group, it shows the name and current value in memory. It formats each value according to the type of each variable, and writes the report so thatNAMELIST
input can read it.The syntax of
NAMELIST
WRITE
is:
WRITE (
extu, namelist-specifier[,
iostat] [,
err])
where namelist-specifier has the form:
[NML=]
group-nameand group-name has been previously defined in a
NAMELIST
statement.The
NAMELIST
WRITE
statement writes values of all variables in the group, in the same order as in theNAMELIST
statement.Example:
NAMELIST
output:
Note that if you do omit the keyword
NML
then the unit parameter must be first, namelist-specifier must be second, and there must not be a format specifier.The
WRITE
can have the form of the following example:
WRITE ( UNIT=6, NML=CASE )Input Actions
The
NAMELIST
input statement reads the next external record, skipping over column one, and looking for the symbol$
in column two or beyond, followed by the group name specified in theREAD
statement.If the $group-name is not found, the input records are read until end of file.
The records are input and values assigned by matching names in the data with names in the group, using the data types of the variables in the group.
Variables in the group that are not found in the input data are unaltered.
The syntax of
NAMELIST
READ
is:
READ ( extu, namelist-specifier [, iostat] [, err] [, end])where namelist-specifier has the form:
[NML=]
group-nameand group-name has been previously defined in a
NAMELIST
statement.Example:
NAMELIST
input:
CHARACTER*14 SAMPLELOGICAL*4 NEWREAL*4 DELTA, MAT(2,2)NAMELIST /CASE/ SAMPLE, NEW, DELTA, MATREAD ( 1, CASE )In this example, the group
CASE
consists of the variables,SAMPLE
,NEW
,DELTA
, andMAT
. If you do omit the keywordNML
, then you must also omit the keywordUNIT
. The unit parameter must be first, namelist-specifier must be second, and there must not be a format specifier.The
READ
can have the form of the following example:
READ ( UNIT=1, NML=CASE )Data Syntax
The first record of
NAMELIST
input data has the special symbol$
(dollar sign) in column two or beyond, followed by theNAMELIST
group name. This is followed by a series of assignment statements, starting in or after column two, on the same or subsequent records, each assigning a value to a variable (or one or more values to array elements) of the specified group. The input data is terminated with another$
in or after column two, as in the pattern:
$
group-name variable=
value[,
variable=
value,...
] $[END]
You can alternatively use an ampersand (
&
) in place of each dollar sign, but the beginning and ending delimiters must match.END
is an optional part of the last delimiter.The input data assignment statements must be in one of the following forms:
variable=value array=value1[, value2,]... array(subscript)=value1[, value2,]... array(subscript,subscript)=value1[, value2,]... variable=character constant variable(index:index)=character constant
If an array is subscripted, it must be subscripted with the appropriate number of subscripts: 1, 2, 3,...
Use quotes (either
"
or'
) to delimit character constants. For more on character constants, see the next section.The following is sample data to be read by the program segment above:
$case delta=0.05, mat( 2, 2 ) = 2.2, sample='Demo' $The data could be on several records. Here
NEW
was not input, and the order is not the same as in the exampleNAMELIST
statement:
$casedelta=0.05mat( 2, 2 ) = 2.2sample='Demo'$Syntax Rules
The following syntax rules apply for input data to be read by
NAMELIST
:
- The variables of the named group can be in any order, and any can be omitted.
- The data starts in or after column two. It may start in column 1, but this is non-standard.
- There must be at least one comma, space, or tab between variables, and one or more spaces or tabs are the same as a single space. Consecutive commas are not permitted before a variable name. Spaces before or after a comma have no effect.
- No spaces or tabs are allowed inside a group name or a variable name, except around the commas of a subscript, around the colon of a substring, and after the
(
and before the)
marks. No name can be split over two records.- The end of a record acts like a space character.
- Note an exception--in a character constant, it is ignored, and the character constant is continued with the next record. The last character of the current record is immediately followed by the second character of the next record. The first character of each record is ignored.
- The equal sign of the assignment statement can have zero or more blanks or tabs on each side of it.
- Only constant values can be used for subscripts, range indicators of substrings, and the values assigned to variables or arrays. You cannot use a symbolic constant (parameter) in the actual input data.
- Hollerith, octal, and hexadecimal constants are not permitted.
- Each constant assigned has the same form as the corresponding FORTRAN constant.
- There must be at least one comma, space, or tab between constants. Zero or more spaces or tabs are the same as a single space. You can enter:
1,2,3
, or1 2 3
, or1, 2, 3
, and so forth.- Inside a character constant, consecutive spaces or tabs are preserved, not compressed.
- A character constant is delimited by apostrophes (
'
) or quotes ("
), but if you start with one of those, you must finish that character constant with the same one. If you use the apostrophe as the delimiter, then to get an apostrophe in a string, use two consecutive apostrophes.- Example: Character constants:
sample='use "$" in 2'
(Read as:use $ in 2
)sample='don''t'
(Read as:don't
)sample="don''t"
(Read as:don''t
)sample="don't"
(Read as:don't
)A complex constant is a pair of real or integer constants separated by a comma and enclosed in parentheses. Spaces can occur only around the punctuation.
A logical constant is any form of true or false value, such as
.TRUE.
or.FALSE.
, or any value beginning with.T
,.F
, and so on.A null data item is denoted by two consecutive commas, and it means the corresponding array element or complex variable value is not to be changed. Null data item can be used with array elements or complex variables only. One null data item represents an entire complex constant; you cannot use it for either part of a complex constant.
Example:
NAMELIST
input with some null data:
* nam2.f Namelist input with consecutive commasREAL ARRAY(4,4)NAMELIST /GRID/ ARRAYWRITE ( *, * ) 'Input?'READ ( *, GRID )WRITE ( *, GRID )ENDThe data for
nam2.f
is:
$GRID ARRAY = 9,9,9,9,,,,,8,8,8,8 $This code loads 9s into row 1, skips 4 elements, and loads 8s into row 3 of
ARRAY
.Arrays Only
The forms r
*
c and r* can be used only with an array.The form r*c stores r copies of the constant c into an array, where r is a nonzero, unsigned integer constant, and c is any constant.
Example:
NAMELIST
with repeat-factor in data:
* nam3.f Namelist "r*c" and "r* "REAL PSI(10)NAMELIST /GRID/ PSIWRITE ( *, * ) 'Input?'READ ( *, GRID )WRITE ( *, GRID )ENDThe input for
nam3.f
is:
$GRID PSI = 5*980 $The program,
nam3.f
, reads the above input and loads 980.0 into the first 5 elements of the arrayPSI
.
- The form r* skips r elements of an array (that is, does not change them), where r is an unsigned integer constant.
Example:
NAMELIST
input with some skipped data.The other input is:
$GRID PSI = 3* 5*980 $The program,
nam3.f
, with the above input, skips the first 3 elements and loads 980.0 into elements 4,5,6,7,8 ofPSI
.Name Requests
If your program is doing
NAMELIST
input from the terminal, you can request the group name andNAMELIST
names that it accepts.To do so, enter a question mark (
?
) in column two and press Return. The group name and variable names are then displayed. The program then waits again for input.Example: Requesting names:
Sun Microsystems, Inc. Copyright information. All rights reserved. Feedback |
Library | Contents | Previous | Next | Index |