Presentation is loading. Please wait.

Presentation is loading. Please wait.

CHAPTER 5 INPUT/OUT FORMAT. Introduction READ (*,*) x WRITE (*,*) x This is free format: the first * is for I/O device number (* = input is keyboard *

Similar presentations


Presentation on theme: "CHAPTER 5 INPUT/OUT FORMAT. Introduction READ (*,*) x WRITE (*,*) x This is free format: the first * is for I/O device number (* = input is keyboard *"— Presentation transcript:

1 CHAPTER 5 INPUT/OUT FORMAT

2 Introduction READ (*,*) x WRITE (*,*) x This is free format: the first * is for I/O device number (* = input is keyboard * = output is the screen) The second * means free format Input devices: keyboard, files Output devices: screen, printer, files

3 Formatting Descriptors (Fields) I field for integers ******************************** Integer :: index=-12,junk=4,number = -12345 Write (*,200) index, index+12, junk, number 100 FORMAT (‘ ‘,2I5, I6,I10) **********************OR ********** Integer :: index=-12,junk=4,number = -12345 Write (*,’(1x,2I5, I6,I10)’) index,index+12,junk,number ****************************************** READING is similar to WRITING EXCEPT writing is right- justified. In reading the numbers anywhere within the field length are read

4 F Descriptors (for REAL) REAL:: a =12.3, b =.123, c=123.456 WRITE (*,100) a,b,c 100 FORMAT (‘ ‘,3F10.2) ************************************************** READING is similar to writing EXCEPT, writing is right-justified. In reading the numbers anywhere within the field length are read. Also it is important to insert the decimal point for the variable to be read.

5 E Descriptors (for exponential REAL) For large and small numbers REAL :: a =1.2346E6, b=0.001, c=-77.7E10 REAL :: d =-77.7E10 WRITE (*,’(1x, 2E14.4,E13.6,E11.6)’)a,b,c,d OUTPUT 0.1235E+07 0.1000E-02- 0.777000E+12*********** ******************************************* Length of field w at least 7 + number of digits after decimal point 1 for sign of mantissa (e.g. -0.777000e12), 2 for zero and decimal point, 1 for E, 1 for sign of exponent, 2 for exponent itself) *************************************************************************** READING is similar to writing EXCEPT, writing is right-justified. In reading the numbers anywhere within the field length are read. Also it is important to insert the decimal point and show the exponent for the variable to be read.

6 ES Descriptors (for exponential REAL Using Scientific and Engineering Notations) Similar to E descriptor For large and small numbers REAL :: a =1.2346E6, b=0.001, c=-77.7E10 REAL :: d =-77.7E10 WRITE (*,’(1x, 2ES14.4,ES13.6,ES11.6)’)a,b,c,d OUTPUT 1.2346E+06 1.0000E-03-7.770000E+11***********

7 A Descriptors (for Characters) Character (len = 17)::name=‘this is a mohamed’ Character (len = 17)::name='this is a mohamed' Write (*,10) name Write (*,11)name Write (*,12) name 10 format (' ',A) 11 format (' ',A20) 12 Format (' ',A6) ****************************** this is a mohamed this i **************************************************************

8 A Descriptors (for Characters) Character (len=10):: string_1, string_2 Character (len=5) :: string_3 ; character (len=15):: string_4,string_5 Read (*,’(A)’)string_1; Read (*,’(A10)’)string_2; Read (*,’(A10)’)string_3 Read (*,’(A10)’)string_4; Read (*,’(A)’)string_5 Write (*,*) string_1,string_2,string_3,string_4,string_5 ******************************************** Input: abcdefghijklmno Abcdefghijklmno ******************************* Output: abcdefghij fghij abcdefghij abcdefghijklmno

9 Other Descriptors X = for space T = for position / = for new line

10 Reading from a file OPEN (UNIT=3, FILE=‘input’, STATUS='OLD', ACTION='READ', & IOSTAT=status ) Read (3,*) x CLOSE ( UNIT=3 ) readloop: DO READ (3,*,IOSTAT=status) value ! Get next value IF ( status /= 0 ) EXIT ! EXIT if not valid. nvals = nvals + 1 ! Valid: increase count WRITE (*,1010) nvals, value ! Echo to screen 1010 FORMAT (' ','Line ', I6, ': Value = ',F10.4 ) END DO readloop

11 Writing to file OPEN (UNIT=4, FILE=‘output’, STATUS=‘NEW', ACTION=‘WRITE', & IOSTAT=status ) Write 4,100) x,y,z 100 format (1x,” x = “, f10.2, “ y = “, f10.2, “ z = “, f10.2)

12 Example PROGRAM table IMPLICIT NONE INTEGER :: cube ! The cube of i INTEGER :: i ! Index variable INTEGER :: square ! The square of i REAL :: square_root ! The square root of i OPEN(UNIT=10,FILE='table.txt',STATUS='unknown') ! Print the title of the table. WRITE (10,100) 100 FORMAT (1x, T4, 'Table of Square Roots, Squares, and Cubes') ! Print the column headings after skipping one line. WRITE (10,110) 110 FORMAT (1x, T4,'Number',T13,'Square Root',T29,'Square',T39,'Cube') WRITE (10,120) 120 FORMAT (1X,T4,'======',T13,'===========',T29,'======',T39,'===='/) ! Generate the required values, and print them out. DO i = 1, 10 square_root = SQRT ( REAL(i) ) square = i**2 cube = i**3 WRITE (10,130) i, square_root, square, cube 130 FORMAT (1X, T4, I4, T13, F10.6, T27, I6, T37, I6) END DO END PROGRAM table

13


Download ppt "CHAPTER 5 INPUT/OUT FORMAT. Introduction READ (*,*) x WRITE (*,*) x This is free format: the first * is for I/O device number (* = input is keyboard *"

Similar presentations


Ads by Google