Formatting input and output - McGill University School of

Download Report

Transcript Formatting input and output - McGill University School of

Example in Final Fall 2006
PROGRAM EXAM
IMPLICIT NONE
INTEGER :: A=3, B=8
REAL :: CALC
B = CALC(A, B)
A = CALC(A, B)
WRITE(*,*) A, B
END PROGRAM EXAM
a) 0
0
b) 3
0
c) 0
3
d) 0
8
e) None of the above
REAL FUNCTION CALC(X, Y)
IMPLICIT NONE
INTEGER :: X, Y
IF(X > Y) THEN
Y = Y/X
CALC = Y
ELSE
CALC = Y/X
END IF
END FUNCTION CALC
Example in Midterm Fall 2006
PROGRAM midterm
IMPLICIT NONE
INTEGER :: a, b
a = 15
b=5
CALL mysub(a, b)
WRITE(*,*) b-a
END PROGRAM
a). -6532
b). -150
c). 1
d). 2356
e). None of the above
SUBROUTINE mysub(b,a)
IMPLICIT NONE
INTEGER a, b, i, j
IF(a > b) THEN
a = (a - 10)**2
b = b*b
ELSE
a = a*b
b = b*b
END IF
if(a == b) THEN
DO i=1, 15
DO j=i, 25-i
a = a + i*j
b=b-a
END DO
END DO
IF(a /= b) THEN
a=2
b=3
END IF
END IF
END SUBROUTINE
Formatting input and
output
Yi Lin
Why formatting?
PROGRAM test
IMPLICIT NONE
REAL::x=1.1, y=1100.1003
Output:
1.100000
1100.100
WRITE(*,*) x
WRITE(*,*) y
END PROGRAM
Nicer format:
1.1000
1100.1003
FORMAT statement
Syntax
write(*, label) list-of-variables
label format format-code
 Semantics


Output list-of-variables according to the formatcodes specified on the line labeled.
FORMAT statement, example 1
Example
REAL::y=1100.1003
label
write(*, 900) y
900 format (F10.4)


format-code
F10.4 means that the number y should be printed using fixed
point notation with field width 10 and 4 decimal places.
Decimal digits=4
#1100.1003
Width=10 (including “.”, white spaces will be filled on the leftmost side)
FORMAT statement, example 2

Example
REAL::x=1.0, y=1100.1003
write(*, 900) x, y
900 format (F3.1, F10.4)


List-of-variables
F3.1 is for x and F10.4 is for y correspondingly.
1.0#1100.1003
(#: white space)
Width should be larger enough!
FORMAT statement, example 4

Example
REAL::x=1.0, y=1100.1003
write(*, 900) x, y
900 format (F3.1, F9.4)



(F3.1,F9.4):
(F3.1,F10.4):
(F3.1,F8.4):


1.01100.1003
1.0#1100.1003
1.0********
*: Width=8 is not wide enough to output y.
4 integer digits + 4 decimal digits + 1 for “.” = 9 digits
Common format codes

E.g., 900 format (F10.4)

The most common format code letters are:
F - real numbers, fixed point format
I - integer
A - text string
E - real numbers, exponent notation
X - horizontal skip (space)
/ - vertical skip (newline)
FORMAT statement, Fw.d

The format code F (and similarly D, E) has the general form Fw.d
where w is an integer constant denoting the field width and d is
an integer constant denoting the number of decimal digits.
d=4
F10.4
#1100.1003
w=10 (including “.”)

If a number or string does not fill up the entire field width, spaces
will be added. Usually the text will be adjusted to the right, but
the exact rules vary among the different format codes.
FORMAT statement, Exponent notation
E: Exponent notation
REAL::y=1100.1003
WRITE(*,100) y
100 FORMAT(E10.2)

d=2
##0.11E+04
w=10 (including “.”)
FORMAT statement, example 5
For integers only the field width is specified, so
the syntax is Iw. Similarly, character strings can
be specified as Aw but the field width is often
dropped.
INTEGER::a=1000
A5
I6
WRITE(*,100) “a=“, a
###a=##1000
100 FORMAT(A5,I6)
A I4
WRITE(*,200) “a=“,a
200
FORMAT(A,I4)
a=1000
WRITE(*,300) “a=“,a
A I3
300 FORMAT(A,I3)
a=***

FORMAT statement, horizontal
skip

nX: horizontally skip n spaces
INTEGER::a=1000
WRITE(*,100) “a=“, a
100 FORMAT(A, 4X, I4)

If n is omitted, n=1
100 FORMAT(A,X,I4)
A
4X
I4
a=####1000
A X I4
a=#1000
FORMAT statement, vertical skip

n/: vertically skip n lines
A
INTEGER::a=1000
WRITE(*,100) “a=“, a
100 FORMAT(A, 2/, I4)

If n is omitted, n=1
2/
a=
#
#
1000
I4
FORMAT statement, repeating
nIw: = repeat Iw n times
INEGER::a=1, b=10, c=100
WRITE(*,100) a,b,c
100 FORMAT(3I4)

I4
I4
I4
###1##10#100
• And similarly nFw.d
FORMAT statement, repeating (cont.)
n(format-codes): = repeat format-codes n
times
FORMAT(3(I4,F10.4))
Is equivalent to
FORMAT(I4,F10.4,I4,F10.4,I4,F10.4)

FORMAT statement, Simplified form


Format strings in read/write statements
write (*,”(A, F8.3)”) “The answer is x = “, x
is equivalent to
write (*,990) “The answer is x = “, x
990 format (A, F8.3)

Sometimes text strings are given in the format
statements, e.g. the following version is also
equivalent:
write (*,999) x
999 format (“The answer is x = “, F8.3)
FORMAT statement, READ

All of these format-codes are also applied to
READ statement.
INTEGER::a,b
READ(*,*) a,b

From the keyboard, we just need to type
1,2 (or 1#2)

! a=1, b=2
But with formatting input, we must be careful
about the number of spaces input.
FORMAT statement, READ

Example
100
INTEGER::a,b
READ(*,100) a,b
FORMAT(2I3) ! eqv. To FORMAT(I3,I3)


If inputting “1,2”, the computer will take the first 3
characters “1,2” and assign them to a. Runtime
error! “1,2” not an integer
If input “##1, 2”, a=##1. But “,2###” will be
assigned to b. Runtime error!
FORMAT statement, READ

Correct inputs for (2I3), e.g.,




“##1##2”
“1##2##”
“#1##2#”
a=##1, b=##2
a=1##, b=2##
a=#1#, b=#2#
What if “#1#22222”?

a=#1#, b=222
FORMAT statement, READ

Correct inputs for READ(*, “(F5.1)”) x?



“##3.4”
 x=3.4
“123.456”
 x=123.4
“12345”
 x=1234.5 (take the leftmost 5
digits first, then assign the last digit as decimal
part and the leftmost 4 digits as integer part)
Format Read

Example
INTEGER::a, b,c
READ(*,100) a,b,c
100FORMAT(I3,x,I2,2x,I4)
•
•
•
•
Input: 123456789
A=123
B=56
C=9
File input/output


READ(*,*)/WRITE(*,*) only reads from/writes to
standard input(e.g., keyboard)/output(screen).
Files: a data storage unit in hard disks.




E.g., HelloWorld.f90
E.g., studentRecords.txt
E.g., experimentalData.txt
We need to read data from existing files and/or write
data to files.
File input/output
Three steps to use a file

Open a file
Input/output using READ and WRITE
1.
2.
–
–
3.
READ: read data from the opened file
WRITE: write data to the opened file
Close the file (A file that has not been closed
can usually not be read. )
File input/output, OPEN

To open a file, syntax:
OPEN ([olist] )
 where, olist is a list of keyword clauses separated
by “,”:
keyword "=" value {"," keyword "=" value}

Example:
OPEN(UNIT=10, FILE=“expData.txt”)
File input/output, OPEN

Important keywords:
[UNIT=] u
FILE= fname

! u is a unique number to identify the file
! File name
Some other keywords (not required in course
materials)
STATUS, ERR, ISOTAT, ACCESS, FORM, RECL, POSITION
File input/output, OPEN

[UNIT=]u



OPEN(10, “expData.txt”)
OPEN(UNIT=10, “expData.txt”)
Unit number (i.e., u) is an integer from 1-99, with
some reserved:


5: standard input
 READ(*,*) == READ(5,*)
6: standard output
 WRITE(*,*) == WRITE(6,*)
FILE input/output, READ/WRITE

READ
label
or
READ(unit, label) list-of-variables
format(format-codes)
READ(unit, *) …

WRITE
label
or
WRITE(unit, label) list-of-variables
format(format-codes)
WRITE(unit, *) …
FILE input/output, CLOSE

A file that has not been closed can usually
not be read.

Syntax:
CLOSE ([UNIT=]u)

For example:
CLOSE (10)
CLOSE (UNIT=10)
FILE input/output, Example
! Input 10 integers from keyboard and write them to file “inputData.txt”
PROGRAM fileTest
IMPLICIT NONE
INTEGER::count, a
OPEN(UNIT=10,FILE=“inputData.txt”) ! Open file “inputData.txt”
DO count=1,10
WRITE(*,*) “Input an integer number from keyboard:”
READ(*,*) a
WRITE(10,100) “a=“, a
! Write to “inputData.txt”
END DO
CLOSE(10);
! Close file “inputData.txt”
FORMAT(A2, I8)
END PROGRAM
a=######51
a=#######6
…
100
inputData.txt
Example in Midterm Fall 2006
program mid
implicit none
integer :: i
character*1 :: a(4)
read(*,7) (a(i), i = 1,4)
7 format (4(A1,X))
write(*,8) (a(i), i = 4,1,-1)
8 format (4A1)
end program
1.
2.
3.
4.
5.
There will be an error
message because
the input string is too
long
pmoc
iumc
cmui
None of the above.