Download presentation
Presentation is loading. Please wait.
1
11/06/20151 FORTRAN 77 Programming. Lecture 5 : January 2001 Dr. Andrew Paul Myers
2
11/06/20152 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 = 7.2345121cm
3
11/06/20153 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. Lines that should fit on the screen “wrap around” onto the next line, because of this padding.
4
11/06/20154 Formatted I/O. The solution to our problem is: Formatted I/O (Input, Output) You have seen formatted input already! READ’(A10)’,character_string
5
11/06/20155 Formatted I/O. The general form of formatted I/O Statements is as follows: PRINT’( )’, READ’( )’, is a format specifier.
6
11/06/20156 Format Specifiers. Format specifiers for variables consist of a letter and a digit(s). A : Character variable. I : Integer variable. F : Real variable. E : Real variable, exponential form.
7
11/06/20157 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.
8
11/06/20158 F6.2 again. All these numbers are in F6.2 format. NumberC1C2C3C4C5C6 345.19345.19 1.2 1.20 -23.45-23.45 5 5.00 9999.99******
9
11/06/20159 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
10
11/06/201510 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
11
11/06/201511 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)
12
11/06/201512 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’
13
11/06/201513 Reading and Writing. Once a data file is opened use READ And WRITE statements to access the data file. READ(, ), WRITE(, ),variable(s)>
14
11/06/201514 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)
15
11/06/201515 Closing a data file. CLOSE(UNIT= | ) CLOSE(UNT=20) CLOSE(20) Close data files when you have finished with them!
16
11/06/201516 File Pointer. The file pointer is positioned at the beginning of a data file when it is first opened. REWIND(UNIT= | ) Moves the file pointer to the start. REWIND(20) or REWIND(UNIT=20)
17
11/06/201517 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.
18
11/06/201518 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?
19
11/06/201519 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
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.