Title: Chapter 9 Files: pp 282291
1Chapter 9 Files pp 282-291
- -Create a file with file_name file_in (remove
the .txt) for - reading data and file_name file_out for
writing data (remove the .txt) - -Fortran 90 allows to
- Connect file to program
-
- Use statement in program
-
- e.g.READ (A), in_file
- OPEN(UNIT9, FILE file_in)
29.1 Files and records
2. e.g.OPEN(UNIT7,STATUSSCRATCH,
FORMUNFORMATED) e.g.
UNFORMATD FILE OPERATIONS WRITE
(UNIT3, IOSTATIOS) X,Y,Z,a,b,c
FORMATED FILE OPERATIONS WRITE
(UNIT3, FMT(2I8/(4F12.4)))X,Y,Z,a,b,c
STATUS OLD, NEW, DATAFILE etc
3- 3. IOSTAT checks if file ok e.g.
- OPEN (UNIT3, FILEfile_in, IOSTAT ios)
- IF(ios /0) THEN
- PRINT, ERROR OPENING file_in
- STOP
- ENDIF
- 4. We can mark the end of file by
- ENDFILE unit e.g. ENDFILE 3
- 5. ACTIONREAD OR ACTIONWRITE
49.3 (18)
(3) Solution (p290.f) PROGRAM Radioactive_decay
IMPLICIT NONE ! This program processes
experimental data relating to ! radioactive
decay which is stored in a file whose name !
is supplied at execution time ! Constant
declarations ! max_readings is maximum number
of sets of data ! in and out are the unit
numbers for reading and writing INTEGER,
PARAMETER max_readings1000,
in3, out6
59.3 (19)
! Variable declarations INTEGER alpha,
beta, gamma, i, ios, max_interval0 REAL
time, last_time0.0, period, av_alpha,
av_beta, av_gamma, max_av_gamma0.0 CHAR
ACTER(LEN20) data_file_name ! Obtain name of
data file
69.3 (20)
DO ! i 1,100 PRINT , "Please give name
of data file" READ '(A)',
data_file_name ! Open data file on unit
number "in" OPEN (UNITin,
FILEdata_file_name, STATUS"OLD",
IOSTATios) ! Repeat request if file not
opened satisfactorily IF (ios 0) EXIT
! Exit when succeed PRINT , "Unable
to open file - please try again" END DO
79.3 (21)
! Print headings WRITE (UNITout, FMT201)
! Process max_readings sets of data in a loop
DO i1, max_readings READ
(UNITin,FMT101) time, alpha, beta, gamma
! Calculate interval since last readings
period time-last_time last_time
time
89.3 (22)
! Calculate average rates of emission
av_alpha alpha/period av_beta
beta/period av_gamma gamma/period
! Print statistics for this interval
WRITE (UNITout, FMT202) i, period, alpha,
beta, gamma,
av_alpha, av_beta, av_gamma
99.3 (23)
! Check for maximum gamma radiation in this
period IF (av_gamma gt max_av_gamma) THEN
max_av_gamma av_gamma
max_interval i END IF END DO !
Print details of interval with maximum gamma
radiation WRITE (UNITout, FMT203)
max_av_gamma, max_interval
109.3 (24)
! Format statements 101 FORMAT (F8.2, 3(5X,
I6)) 201 FORMAT ("1", "Interval", T11,
"Time", T17,"Alpha", T24, "Beta",
T30, "Gamma", T37, "Average", T46,"Average",
T55,"Average"/ T38, "Alpha", T47,
"Beta", T56, "Gamma") 202 FORMAT (I6, F8.2,
2I6, I7, 2F9.2, F10.2) 203 FORMAT ("0", T3,
"Maximum average gamma radiation was",
F7.2, " in interval", I5) END PROGRAM
Radioactive_decay Note, that it was not
necessary to open unit out since this was
assumed to be the default output unit.