330 likes | 482 Views
Basic I/O. Input and Output. The READ Statement Basic Version. Syntax. Performs a list-directed read from the file (or device) indicated by the unit number value. READ ( unit_num, * ) input_list. The WRITE Statement Basic Version. Syntax.
E N D
Basic I/O Input and Output
The READ Statement Basic Version Syntax • Performs a list-directed read from the file (or device) indicated by the unit number value READ (unit_num, *) input_list
The WRITE Statement Basic Version Syntax • Performs a list directed write to the file (or device) indicated by the unit number value WRITE (unit_num, *) output_list
Default Units • External devices (keyboard, screen, file, etc.) are called units in Fortran. • A default unit will be used for input or output unless the program indicated otherwise. • On most current systems, • the keyboard is the default input unit, and • the screen is the default output unit.
Unit Numbers • In Fortran, a unit number is used to indicate each unit that a program plans to access. • A unit number is an unsigned integer, generally from 0 to 99.
PRINT versus WRITE • PRINT only directs output to the standard output unit • WRITE can direct output to any output unit and any file type
Write Statement • The unit-specifier is an integer expression whose value designate the output device. • UNIT = unit-specifier • unit-specifier
Write Statement • The Format Specifier may be of any of the forms allowed in the print statement • Examples: • WRITE(6,*) A, B, C • WRITE(UNIT=6, FMT=*) A, B, C • WRITE (*,*) A, B, C
Formatted Output • Examples: • WRITE(6, ‘(1X, 3I5)’) A, B, C • WRITE(6, FMT = ‘(1X, 3I5)’) A, B, C • WRITE(6, 99) A, B, C99 FORMAT (1X, 3I5)
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
WRITE Statement • The ADVANCE = clause is used to specify whether output should advance to a new line after the current output has been completed. • ADVANCE = “NO”causes non-advancing output • ADCANCE = “YES”is the default condition, causes an advance to a new line
WRITE Statement • ADVANCE ExampleWRITE (*, '(1X, A)', ADVANCE = "NO") "Enter name of data file: " READ *, FileName Enter name of data file: temp.dat
READ Statement • READ (Control - List) • Unit specifier • Format specifier • ADVANCE = clause • IOSTAT = clause • to detect an input error • END = clause • to detect the end of file • Other items to process files
READ Statement • EXAMPLES • READ (5, *) A, B, C • READ (5, FMT= *) A, B, C • READ (UNIT = 5, FMT = *) A, B, C • READ (*, *) A, B, C
Formatted Input • EXAMPLES: • READ (5, ‘(I6, 2F6.1)’) N, A, Z • READ (5, FMT= ‘(I6, 2F6.1)’ ) N, A, Z • READ (5, 20) N, A, Z20 FORMAT (I6, 2F6.1)
FILE I/O • The Major Statements • OPEN • CLOSE • READ • WRITE
The OPEN StatementBasic Version Syntax • Associates a unit number with a specific file or peripheral device • Makes the file available for use • Example:OPEN (UNIT = 12, FILE = Filename , STATUS = “NEW”) OPEN (UNIT = unit_num, FILE = file_name, STATUS = status_word )
OPEN Statement • OPEN (open - list) • Unit Specifier • FILE = clause (name of the file being opened) • STATUS = clause (status of the file) • ACTION = clause (I/O type) • POSITION = clause (position the file) • IOSTAT = clause (error check)
Unit Specifiers • Every compiler reserves a few unit numbers for special purposes including standard input and output. • standard (default) input is normally 5 • standard (default) output in normally 6 • The programmer must select an arbitrary unit number for every other unit.
The FILE Argument • file_name is the name by which the file is known to the operating system. • file_name can be a character literal or the name of a character variable which contains the actual file name.
The STATUS Argument • status_word may be one of four values • “NEW” • File does not exist • File will be created by program • Normally used with report files • “OLD” • File exists • Normally used with data files • “REPLACE” • Creates a new file replacing the old one.
Action = clause • ACTION = i_o_action • “READ” • “WRITE” • READWRITEThe file will be opened for reading only, for writing only, or for both reading and writing.
POSITION = clause • POSITION = character_expression • “REWIND” • “APPEND” • “ASIS”These specifiers position the file at its initial point, at the end of the file, or leave its position unchanged.
IOSTAT = clause • IOSTAT = status_variable • Where status variable in an integer variable • 0 (Zero)File opened successfully • > 0 (Positive Value)Error opening File • < 0 (Negative Value)End of file detected
OPEN Example • OPEN (UNIT = 25, FILE = “temp.dat”, & STATUS = “OLD”, & ACTION = “READ”, & POSITION = “REWIND”, & IOSTAT = OpenStatus)
File Input Example • EXAMPLE:CHARACTER(20) :: FileNameWRITE (*, '(1X, A)', ADVANCE = "NO") "Enter name of data file: ”READ *, FileNameOPEN (UNIT = 15, FILE = FileName, STATUS = "OLD", & IOSTAT = OpenStatus)IF (OpenStatus > 0) STOP "*** Cannot open the file ***"
File Input Example • DO READ (UNIT = 15, FMT = 100, IOSTAT = InputStatus) & Temperature, Volume IF (InputStatus > 0) STOP "*** Input error ***” IF (InputStatus < 0) EXIT ! end of fileEND DO
File Output Example • OPEN (UNIT = 21, FILE = “MyReport” , STATUS = ”NEW", & IOSTAT = OpenStatus)WRITE (21, ‘(1X, I5, 2F7.2)’ ) Code, Temp, Pressure
The CLOSE Statement Basic Version Syntax • Removes the association between a file and a unit number • Strongly recommended, but not required • Should be done as soon as the program is finished with the file. CLOSE (UNIT = unit_num)
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, "===========", "======"
! 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
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
Input File 1200034203221015 1300038803221121 1400044803241425 1500051303201520 1600055503181665 1700061303191865 1800067503232080 1900072103282262 2000076803252564 2100083503272869 2200088903303186