The Queen's University of Belfast

Parallel Computer Centre
[Next] [Previous] [Top]
Interactive input and output
Interactive input and output
Topics
- I/O Overview.
- FORMAT Statement.
- Edit Descriptors.
- Input/Output Lists.
- Namelist.
- Non-Advancing I/O.
I/O Overview
Background

- Data may be input or output:
- default form, or
- user defined form - formatting.
FORMAT Statement
- Used to read or write data in a form other than the default format.
- I/O is controlled using edit descriptors.
- A labelled statement -
WRITE
and READ
statements reference the format statement label number
- General form:
label FORMAT( list )
where list
is a list of edit descriptors drawn from:
I,F,E,ES,EN,D,G,L,A,H,T,TL,TR,X,P,
BN,BZ,SP,SS,S,/,:,'
Examples:
READ(*,100) i,j
WRITE(*,100) i,j
READ(*,FMT=200) x,y
WRITE(*,200) x,y
100 FORMAT(2I)
200 FORMAT(2F10.6)
- Format may be placed directly in I/O statements:
READ(*,'(2I)') i,j
WRITE(*,'(2F12.6)') x,y
Edit Descriptors
- Specify exactly how values should be converted into a character string.
- Only the following are covered.
I,F,E,ES,EN,A,X,/,:,',()
- The following terms are used in the edit descriptor definitions:
- a - repeat count
- w - width of field - total number of characters
- m - number of digits
- d - digits to right of decimal point
- e - number of digits in exponent
I
- I - integer data
- General forms: I, Iw, Iw.m, aI, aIw, aIw.m
- If w is too small or if w => m then w asterisks is printed
- Examples:
I I6 I10.3 5I 4I6.4
- I6.4 specifies a total of 6 characters including a sign and leading spaces, if necessary, but at least 4 digits thus:
WRITE (6,`(I6.4)`) 56
would ouput two spaces followed by 0056
F
WRITE(6,`(2F12.6)`) 12.6, -131.45678
would output:
^^^12.600000 -131.456780
E, ES, EN
- Floating point notation for real data.
- E, Ew, Ew.d, aE, aEw, aEw.d
- w includes signs and decimal point.
- Specifies a number in the form S0.XXXESXX where S signifies a sign, X digits and E identifies the exponent.
- The most significant digit is placed to the right of the decimal point e.g. 0.123E-2
- EN - exponent is divisible by 3.
- ES - most significant digit is placed to right of the decimal point.
A, X, L
- Input or output of single characters or strings
- A, Aw, aAw
- If w is greater than required then characters are right justified and leading spaces inserted.
- On input the string does not need to be in quotes
- Skip specified number of characters
- Logical data
- L, aL
- On input T, F, .TRUE., FALSE. are acceptable
- On output T or F will be written
Special characters
- / take a new line
- : terminate I/O if list is exhausted
- () group descriptors - usually for repetition, for example 4(2X,F12.6)
- ` output the specified character string, for example (`Area of circle =', F12.6)
Input/Output Lists
Implied DO Loop
- Useful for I/O with arrays.
- General form:
(do_list, var=start,stop[,step])
- Similar to the indexed DO loop.
- Examples:
INTEGER :: i, j
REAL, DIMENSION(10) :: a
REAL, DIMENSION(10,10) :: b
READ(*,*) (a(j),j=1,10)
WRITE(*,*) (a(j), j=10,1,-1)
WRITE(*,*) ((b(i,j),i=1,10), j=1,10)
The last WRITE
satatement prints b(1,1)
, b(2,1)
, b(3,1)
, ...etc.
Namelist
- Facility for grouping variables for I/O.
- Complex rules govern use - input, pointers and allocatable arrays are not covered.
- Most useful for outputting debugging information.
- Use
NAMELIST
statement to group variables.
- General form:
NAMELIST /group_name/ variable_list
Examples
NAMELIST /week/ mon, tues
- The list may extended within the same scoping unit by repeating the
group_name
on another statement, as follows:
NAMELIST/week/ sat, sun, thur
mon = 2
tues = 3
sat = 1
WRITE(*,NML=week)
will produce:
&WEEK MON=2, TUES=3, SAT=1.../
- The output record has the general form.
&group-name var=value [,var=value]/
- This form is required for input.
- Arrays may also be specified:
INTEGER, DIMENSION(10) :: items
NAMELIST /group/items
items(1) = 1
...
WRITE(*,NML=group)
would produce:
&group items(1)=1, items(2:10)=0 /
Non-Advancing I/O
- Inhibit normal advance to next record.
- Read long records in sections.
- Response appears on the same line as prompt.
- Use
ADVANCE
keyword:
READ(*,*,ADVANCE='YES') ...
WRITE(*,*, ADVANCE='NO') ...
- Optional Keywords
- EOR=label
- SIZE=integer-variable
- Padding with blanks (
OPEN
statement)
Examples
WRITE(*,*,ADVANCE='NO') 'value? '
READ(*,*) I
This would appear as
value? 10
CHARACTER(LEN=32) :: filename
INTEGER :: length
bb: DO
WRITE(*,*, ADVANCE='NO') 'File? '
READ(*,*,ADVANCE='NO',EOR=20,&
SIZE=length) filename
OPEN(10,FILE=filename(1:LENGTH))
EXIT bb
20 WRITE(*,*)'error...'
END DO
[Next] [Previous] [Top]
All documents are the responsibility of, and copyright, © their authors and do not represent the views of The Parallel Computer Centre, nor of The Queen's University of Belfast.
Maintained by Alan Rea, email A.Rea@qub.ac.uk
Generated with CERN WebMaker