basic i/o

33
Basic I/O Input and Output

Upload: hera

Post on 02-Feb-2016

37 views

Category:

Documents


0 download

DESCRIPTION

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. - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Basic I/O

1

Basic I/O

Input and Output

Page 2: Basic I/O

2

The READ Statement Basic Version

Performs a list-directed read from the file (or device) indicated by the unit number value

Syntax

READ (unit_num, *) input_list

Page 3: Basic I/O

3

The WRITE Statement Basic Version

Performs a list directed write to the file (or device) indicated by the unit number value

Syntax

WRITE (unit_num, *) output_list

Page 4: Basic I/O

4

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.

Page 5: Basic I/O

5

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.

Page 6: Basic I/O

6

PRINT versus WRITE

PRINT only directs output to the standard output unit

WRITE can direct output to any output unit and any file type

Page 7: Basic I/O

7

Write Statement

The unit-specifier is an integer expression whose value designate the output device.

UNIT = unit-specifier unit-specifier

Page 8: Basic I/O

8

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

Page 9: Basic I/O

9

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)

Page 10: Basic I/O

10

Formatted OutputPROGRAM 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

Page 11: Basic I/O

11

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

Page 12: Basic I/O

12

WRITE Statement

ADVANCE Example

WRITE (*, '(1X, A)', ADVANCE = "NO") "Enter name of data file: " READ *, FileName

Enter name of data file: temp.dat

Page 13: Basic I/O

13

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

Page 14: Basic I/O

14

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

Page 15: Basic I/O

15

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)

Page 16: Basic I/O

16

FILE I/O

The Major Statements OPEN CLOSE READ WRITE

Page 17: Basic I/O

17

The OPEN StatementBasic Version

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”)

Syntax

OPEN (UNIT = unit_num, FILE = file_name, STATUS = status_word )

Page 18: Basic I/O

18

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)

Page 19: Basic I/O

19

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.

Page 20: Basic I/O

20

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.

Page 21: Basic I/O

21

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.

Page 22: Basic I/O

22

Action = clause

ACTION = i_o_action “READ” “WRITE” READWRITE

The file will be opened for reading only, for writing only, or for both reading and writing.

Page 23: Basic I/O

23

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.

Page 24: Basic I/O

24

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

Page 25: Basic I/O

25

OPEN Example

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

Page 26: Basic I/O

26

File Input Example

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 ***"

Page 27: Basic I/O

27

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 file

END DO

Page 28: Basic I/O

28

File Output Example

OPEN (UNIT = 21, FILE = “MyReport” , STATUS = ”NEW", &IOSTAT = OpenStatus)

WRITE (21, ‘(1X, I5, 2F7.2)’ ) Code, Temp, Pressure

Page 29: Basic I/O

29

The CLOSE Statement Basic Version

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.

Syntax

CLOSE (UNIT = unit_num)

Page 30: Basic I/O

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, "===========", "======"

Page 31: Basic I/O

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

Page 32: Basic I/O

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

Page 33: Basic I/O

33

Input File

12000342032210151300038803221121140004480324142515000513032015201600055503181665170006130319186518000675032320801900072103282262200007680325256421000835032728692200088903303186