Transcript Document
FORTRAN 77
Programming.
Lecture 5 : January 2001
Dr. Andrew Paul Myers
17/07/2015
1
Screen Output.
So far the output of results to the screen
has been “messy”, because we have been
using “free format”. e.g.
PRINT*,’Radius = ‘,radius,’ cm’
typical screen output :
Radius =
17/07/2015
7.2345121
cm
2
Free Format.
Free format is simple and easy to use. e.g. with the
PRINT* and READ* statements.
Assumes numeric input, therefore limited.
Always uses greatest accuracy possible.
Pads out printed variable and text into columns.
17/07/2015
Lines that should fit on the screen “wrap around” onto
the next line, because of this padding.
3
Formatted I/O.
The solution to our problem is:
Formatted I/O (Input, Output)
You have seen formatted input
already!
READ’(A10)’,character_string
17/07/2015
4
Formatted I/O.
The general form of formatted I/O
Statements is as follows:
PRINT’(<Format>)’,<variable(s)>
READ’(<Format>)’,<variable(s)>
<Format> is a format specifier.
17/07/2015
5
Format Specifiers.
Format specifiers for variables
consist of a letter and a digit(s).
17/07/2015
A : Character variable.
I : Integer variable.
F : Real variable.
E : Real variable, exponential form.
6
Examples.
A10 : String variable 10 characters long.
e.g. ‘Hello ‘.
I8 : Integer, 8 digits long.
F6.2 : Real variable, 2 decimal places, 6 digits
long including decimal points and minus
signs.
17/07/2015
7
F6.2 again.
All these numbers are in F6.2 format.
Number
C1
C2
C3
C4
C5
C6
345.19
3
4
5
.
1
9
1
.
2
0
3
.
4
5
5
.
0
0
*
*
*
*
1.2
-23.45
-
2
5
9999.99
17/07/2015
*
*
8
Examples.
READ’(A30)’,string1
READ’(A30,2I4),string2,num1,num2
PRINT’(“Answer = “,F6.2)’,answer
“ / ” and “ X “ are new line and space.
PRINT’(/“A = “,I2,2X,”B = “,F10.1,//)’,a,b
PRINT’(“Enter a number “,$)’
READ*,number
17/07/2015
9
The old way…
Formatted I/O the old way, with
numeric labels.
100 FORMAT(/’A = ‘,I2,2X,’B = ‘,F10.1//)
PRINT 100,a,b
200 FORMAT(3I5)
READ 200,int1,int2,int3
17/07/2015
10
Data Files.
You must first open a data file.
Then read or write data.
Finally close the data file.
Data files are analogous to books.
Fortran OPEN statement.
OPEN(UNIT=x,FILE=y,STATUS=z)
17/07/2015
11
Opening Data Files.
Choose unit numbers >6
Unit 5 = keyboard and Unit 6 = screen!
OPEN(UNIT=20,FILE=‘data.dat’,STATUS=‘NEW’)
FILE=‘data.dat’
FILE=‘/disk/n/gps/data/data.dat’
FILE=file_name
STATUS=‘NEW’
STATUS=‘OLD’
STATUS=‘UNKNOWN’
17/07/2015
12
Reading and Writing.
Once a data file is opened use READ
And WRITE statements to access the
data file.
READ(<unit>,<format>),<variable(s)>
WRITE(<unit>,<format>),variable(s)>
17/07/2015
13
File I/O Examples.
e.g.
READ(1,*) num1,num2,num3
WRITE(20,’(5X,I5,10X,3F5.1)’) a,b,c,d
READ(25,’(2F10.5)’) data1(loop),data2(loop)
17/07/2015
14
Closing a data file.
CLOSE(UNIT=<unit> | <unit>)
CLOSE(UNT=20)
CLOSE(20)
Close data files when you have finished
with them!
17/07/2015
15
File Pointer.
The file pointer is positioned at the
beginning of a data file when it is
first opened.
REWIND(UNIT=<unit> | <unit>)
Moves the file pointer to the start.
REWIND(20) or REWIND(UNIT=20)
17/07/2015
16
Error Trapping I
IOSTAT : Used to test if a file exists if
opened with ‘OLD’ or ‘UNKNOWN’
status. e.g.
OPEN(IOSTAT=I,UNIT=20,
& FILE=‘test.dat’,STATUS=‘OLD’)
IOSTAT returns an INTEGER value.
17/07/2015
17
Error Trapping II.
Integer IOSTAT values returned are:
0 : File opened without errors.
>0 : Error, file not found?
<0 : As condition 0, but at end of
file (EOF), file empty.
What about EOF during reading data?
17/07/2015
18
Error Trapping III.
Using the END option with a READ
Statement you can test for EOF.
DO WHILE ( .NOT. 0 )
READ(25,’(I5)’,END=100 )data(i)
END DO
100 CONTINUE
17/07/2015
19