Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Basic I/O Input and Output. 2 The READ Statement Basic Version l Performs a list-directed read from the file (or device) indicated by the unit number.

Similar presentations


Presentation on theme: "1 Basic I/O Input and Output. 2 The READ Statement Basic Version l Performs a list-directed read from the file (or device) indicated by the unit number."— Presentation transcript:

1 1 Basic I/O Input and Output

2 2 The READ Statement Basic Version l Performs a list-directed read from the file (or device) indicated by the unit number value Syntax READ (unit_num, *) input_list

3 3 The WRITE Statement Basic Version l Performs a list directed write to the file (or device) indicated by the unit number value Syntax WRITE (unit_num, *) output_list

4 4 Default Units l External devices (keyboard, screen, file, etc.) are called units in Fortran. l A default unit will be used for input or output unless the program indicated otherwise. l On most current systems, n the keyboard is the default input unit, and n the screen is the default output unit.

5 5 Unit Numbers l In Fortran, a unit number is used to indicate each unit that a program plans to access. l A unit number is an unsigned integer, generally from 0 to 99.

6 6 PRINT versus WRITE l PRINT only directs output to the standard output unit l WRITE can direct output to any output unit and any file type

7 7 Write Statement l The unit-specifier is an integer expression whose value designate the output device. n UNIT = unit-specifier n unit-specifier

8 8 Write Statement l The Format Specifier may be of any of the forms allowed in the print statement l Examples: n WRITE(6,*) A, B, C n WRITE(UNIT=6, FMT=*) A, B, C n WRITE (*,*) A, B, C

9 9 Formatted Output l Examples: n WRITE(6, ‘(1X, 3I5)’) A, B, C n WRITE(6, FMT = ‘(1X, 3I5)’) A, B, C n WRITE(6, 99) A, B, C 99 FORMAT (1X, 3I5)

10 10 Formatted Output PROGRAM Table_of_Values INTEGER :: N, LastNumber PRINT *, "Enter last number to be used:" READ *, LastNumber ! Print headings PRINT '(// 1X, A8, T12, A8, T22, A8, T32, A9 / 1X, 40("="))', & "Number", "Square", " Cube", "Sq. root" ! Print the table DO N = 1, LastNumber PRINT '(1X, I6, 2I10, 2X, F10.4)', & N, N**2, N**3, SQRT(REAL(N)) END DO END PROGRAM Table_of_Values

11 11 WRITE Statement l The ADVANCE = clause is used to specify whether output should advance to a new line after the current output has been completed. n ADVANCE = “NO” causes non-advancing output n ADCANCE = “YES” is the default condition, causes an advance to a new line

12 12 WRITE Statement l ADVANCE Example WRITE (*, '(1X, A)', ADVANCE = "NO") "Enter name of data file: " READ *, FileName Enter name of data file: temp.dat

13 13 READ Statement l READ (Control - List) n Unit specifier n Format specifier n ADVANCE = clause n IOSTAT = clause –to detect an input error n END = clause –to detect the end of file n Other items to process files

14 14 READ Statement l EXAMPLES n READ (5, *) A, B, C n READ (5, FMT= *) A, B, C n READ (UNIT = 5, FMT = *) A, B, C n READ (*, *) A, B, C

15 15 Formatted Input l EXAMPLES: n READ (5, ‘(I6, 2F6.1)’) N, A, Z n READ (5, FMT= ‘(I6, 2F6.1)’ ) N, A, Z n READ (5, 20) N, A, Z 20 FORMAT (I6, 2F6.1)

16 16 FILE I/O l The Major Statements n OPEN n CLOSE n READ n WRITE

17 17 The OPEN Statement Basic Version l Associates a unit number with a specific file or peripheral device l Makes the file available for use l Example: OPEN (UNIT = 12, FILE = Filename, STATUS = “NEW”) Syntax OPEN (UNIT = unit_num, FILE = file_name, STATUS = status_word )

18 18 OPEN Statement l OPEN (open - list) n Unit Specifier n FILE = clause (name of the file being opened) n STATUS = clause (status of the file) n ACTION = clause (I/O type) n POSITION = clause (position the file) n IOSTAT = clause (error check)

19 19 Unit Specifiers l Every compiler reserves a few unit numbers for special purposes including standard input and output. n standard (default) input is normally 5 n standard (default) output in normally 6 l The programmer must select an arbitrary unit number for every other unit.

20 20 The FILE Argument l file_name is the name by which the file is known to the operating system. l file_name can be a character literal or the name of a character variable which contains the actual file name.

21 21 The STATUS Argument l status_word may be one of four values n “NEW” –File does not exist –File will be created by program –Normally used with report files n “OLD” –File exists –Normally used with data files n “REPLACE” –Creates a new file replacing the old one.

22 22 Action = clause l ACTION = i_o_action n “READ” n “WRITE” n READWRITE The file will be opened for reading only, for writing only, or for both reading and writing.

23 23 POSITION = clause l POSITION = character_expression n “REWIND” n “APPEND” n “ASIS” These specifiers position the file at its initial point, at the end of the file, or leave its position unchanged.

24 24 IOSTAT = clause l IOSTAT = status_variable n Where status variable in an integer variable n 0 (Zero) File opened successfully n > 0 (Positive Value) Error opening File n < 0 (Negative Value) End of file detected

25 25 OPEN Example l OPEN (UNIT = 25, FILE = “temp.dat”, & STATUS = “OLD”, & ACTION = “READ”, & POSITION = “REWIND”, & IOSTAT = OpenStatus)

26 26 File Input Example l EXAMPLE: CHARACTER(20) :: FileName WRITE (*, '(1X, A)', ADVANCE = "NO") "Enter name of data file: ” READ *, FileName OPEN (UNIT = 15, FILE = FileName, STATUS = "OLD", & IOSTAT = OpenStatus) IF (OpenStatus > 0) STOP "*** Cannot open the file ***"

27 27 File Input Example l DO READ (UNIT = 15, FMT = 100, IOSTAT = InputStatus)& Temperature, Volume IF (InputStatus > 0) STOP "*** Input error ***” IF (InputStatus < 0) EXIT ! end of file END DO

28 28 File Output Example l OPEN (UNIT = 21, FILE = “MyReport”, STATUS = ”NEW", & IOSTAT = OpenStatus) WRITE (21, ‘(1X, I5, 2F7.2)’ ) Code, Temp, Pressure

29 29 The CLOSE Statement Basic Version l Removes the association between a file and a unit number l Strongly recommended, but not required l Should be done as soon as the program is finished with the file. Syntax CLOSE (UNIT = unit_num)

30 30 PROGRAM Temperature_Volume_Readings IMPLICIT NONE INTEGER :: Count = 0, OpenStatus, InputStatus CHARACTER(20) :: FileName REAL :: Temperature, Volume, SumOfTemps = 0.0, SumOfTemps2 = 0.0, & SumOfVols = 0.0, SumOfProds = 0.0, MeanTemperature, & MeanVolume, Slope, Y_Intercept ! Open the file as unit 15, set up the input and output ! formats, and display the table heading WRITE (*, '(1X, A)', ADVANCE = "NO") "Enter name of data file: " READ *, FileName OPEN (UNIT = 15, FILE = FileName, STATUS = "OLD", IOSTAT = OpenStatus) IF (OpenStatus > 0) STOP "*** Cannot open the file ***" 100 FORMAT(4X, F4.1, T13, F4.1) 110 FORMAT(1X, A11, A10) 120 FORMAT(1X, F8.1, F12.1) PRINT * PRINT 110, "Temperature", "Volume" PRINT 110, "===========", "======"

31 31 ! While there is more data, read temperatures and volumes, ! display each in the table, and calculate the necessary sums DO READ (UNIT = 15, FMT = 100, IOSTAT = InputStatus) Temperature, Volume IF (InputStatus > 0) STOP "*** Input error ***" IF (InputStatus < 0) EXIT ! end of file PRINT 120, Temperature, Volume Count = Count + 1 SumOfTemps = SumOfTemps + Temperature SumOfTemps2 = SumOfTemps2 + Temperature ** 2 SumOfVols = SumOfVols + Volume SumOfProds = SumOfProds + Temperature * Volume END DO

32 32 MeanTemperature = SumOfTemps / REAL(Count) MeanVolume = SumOfVols / REAL(Count) Slope = (SumOfProds - SumOfTemps * MeanVolume) / & (SumOfTemps2 - SumOfTemps * MeanTemperature) Y_Intercept = MeanVolume - Slope * MeanTemperature PRINT 130, Slope, Y_Intercept 130 FORMAT(//1X, "Equation of least-squares line is" & /1X, " y =", F5.1, "x + ", F5.1, & /1X, "where X is temperature and y is volume") CLOSE (15) END PROGRAM Temperature_Volume_Readings

33 33 Input File 1200034203221015 1300038803221121 1400044803241425 1500051303201520 1600055503181665 1700061303191865 1800067503232080 1900072103282262 2000076803252564 2100083503272869 2200088903303186


Download ppt "1 Basic I/O Input and Output. 2 The READ Statement Basic Version l Performs a list-directed read from the file (or device) indicated by the unit number."

Similar presentations


Ads by Google