Input and output Input streams n n there

  • Slides: 28
Download presentation
Input and output

Input and output

Input streams n n there are two ways of handling I/o list-directed – done

Input streams n n there are two ways of handling I/o list-directed – done using default settings – PRINT*, num – READ*, num n formatted – controlled by the programmer – you can specify every detail of what is printed

Output statements n PRINT – only used for output to the screen n WRITE

Output statements n PRINT – only used for output to the screen n WRITE – used for output to files

PRINT statements n List directed – PRINT*, num n Formatted – PRINT ‘(1 x,

PRINT statements n List directed – PRINT*, num n Formatted – PRINT ‘(1 x, i 5, f 8. 2)’, number, temperature n With format labels – PRINT 10, number, temperature – 10 FORMAT (1 x, i 5, f 8. 2)

Format descriptors n (1 x, i 5, f 8. 2) – 1 space, followed

Format descriptors n (1 x, i 5, f 8. 2) – 1 space, followed by a 5 -digit integer, followed by a real number taking up 8 spaces total with two after the decimal point. n A format descriptor designated the exact number of characters that will be printed and where they will be printed.

The integer format descriptor n i used for integers, can be repeated (5 i)

The integer format descriptor n i used for integers, can be repeated (5 i) and can have width specified (i 5) – – – INTEGER : : num = 123 PRINT ‘(i 3)’, num PRINT ‘(3 i)’, num PRINT *, num PRINT ‘(i 2)’, num -------------> 123123123 -------------> **

The real number descriptor n F for ‘floating point’ (real) numbers. – Followed by

The real number descriptor n F for ‘floating point’ (real) numbers. – Followed by an integer total field width, ‘. ’, then an integer number of decimal digits. – – – REAL : : num = 123. 45 PRINT ‘(f 8. 2)’, num PRINT ‘(f 6. 2)’ , num PRINT*, num PRINT ‘(f 3. 2)’, num --------> 123. 45 ---------------> 123. 4500 --------> ***

The character data descriptor n a – – – used for ‘alpha-numeric data’ CHARACTER(10)

The character data descriptor n a – – – used for ‘alpha-numeric data’ CHARACTER(10) : : name = “Washington” PRINT ‘(a 10)’, name --------> Washington PRINT ‘(a 15)’, name --------> Washington_____ PRINT*, name --------> Washington PRINT ‘(a 5)’ --------> Washi

The spacing descriptors n n X - stands for ‘print a blank space’ T

The spacing descriptors n n X - stands for ‘print a blank space’ T - stands for ‘print a tab’ – – – INTEGER : : num 1, num 2 num 1 = 123 num 2 = 456 PRINT ‘(1 x, i 5, 5 x, i 3)’, num 1, num 2 ___123_____456 PRINT ‘(t 5, i 3), num 1 --------> _____123

Difference between x and t n n n 5 x gives five spaces t

Difference between x and t n n n 5 x gives five spaces t 5 tabs out to column 5 (gives only 4 spaces!) When used in tables together they are easy to confuse.

Repetition and width n. X if first, specifies line spacing n. X n blanks

Repetition and width n. X if first, specifies line spacing n. X n blanks or spaces printed A number of characters matches list item Aw w characters are printed Iw integer is right-justified in w columns Fw. d real no. is in w columns, d is decimal places

Carriage controls n FIRST EDIT DESCRIPTOR – ‘ ‘ OR 1 X PRINTS A

Carriage controls n FIRST EDIT DESCRIPTOR – ‘ ‘ OR 1 X PRINTS A SPACE – ‘ 0’ DOUBLE LINE SPACING – ‘ 1’ PRINTS ON TOP OF PAGE – ‘+’ PRINTS WITHOUT SPACING (OVERPRINTS) SOME COMPILERS DON’T RECOGNIZE ALL OF THE ABOVE

Repeating Edit Descriptors n Place a number in front of descriptor n. I 4

Repeating Edit Descriptors n Place a number in front of descriptor n. I 4 repeats I 4 n times 3 F 5. 2 repeats F 5. 2 3 times 4(5 X, I 2) repeats 5 X & I 2 4 times

More descriptors Tw / n(/) Ew. d tabs over to column w starts rest

More descriptors Tw / n(/) Ew. d tabs over to column w starts rest of statement on a new line starts rest of statement in n lines reads numbers in scientific notation

Literals n Individual characters may also be inserted in formats. – PRINT ‘(“$”, f

Literals n Individual characters may also be inserted in formats. – PRINT ‘(“$”, f 6. 2)’, price -----> $123. 45 – PRINT ‘(10(“-”))’ --------> =====

Printing tables n n First print the heading Then a loop fills in the

Printing tables n n First print the heading Then a loop fills in the body of the table – PRINT ‘(t 10, “Number”, t 20, “Square”)’ – DO n=1, 5 – PRINT ‘(9 x, i 6, t 20, i 6)’, n, n**2 – ENDDO – – – -----Number----Square ----1 ---1 ----2 ---4 ----3 ---9 ----4 ---16 ----5 ---25

READ statements n List directed – READ*, num n Formatted – READ ‘(1 x,

READ statements n List directed – READ*, num n Formatted – READ ‘(1 x, i 5, f 8. 2)’, number, temperature n With format labels – READ 10, number, temperature – 10 FORMAT (1 x, i 5, f 8. 2)

Reading real numbers n Data may come in in one of two ways –

Reading real numbers n Data may come in in one of two ways – Numbers without decimal points • the decimal digits are always filled – Numbers with decimal points – REAL : : num – READ ‘(f 6. 2)’, num – The data entered may be either with decimal point (123. 45) or not ( 12345)

The WRITE statement n n is used to write to files Underlies the PRINT

The WRITE statement n n is used to write to files Underlies the PRINT statement PRINT*, is really WRITE (*, *) or WRITE (6, *) By default, write will send output to the default output device. – This device is the monitor – It is identified by a number (UNIT = 6) – Often called ‘standard output’

WRITE default examples n n n WRITE (*, *) num 1, num 2 WRITE

WRITE default examples n n n WRITE (*, *) num 1, num 2 WRITE (*, ‘(i 4, 1 x, i 6)’) num 1, num 2 WRITE (*, 20) num 1, num 2 20 FORMAT (1 x. I 4, t 12, i 5) WRITE (UNIT = 6, FMT = 30) num 1, num 2

READ default settings n n is used to read from files By default, read

READ default settings n n is used to read from files By default, read will read from the default input device – This device is the keyboard – It is identified by the number (UNIT = 5) – Often called ‘standard input’

Default devices n n n UNIT 6 is standard output UNIT 5 is standard

Default devices n n n UNIT 6 is standard output UNIT 5 is standard input It is an error to try to read from standard output or write to standard input.

File processing n n Data commonly comes to us in files. We will learn

File processing n n Data commonly comes to us in files. We will learn how to handle – text files (ASCII characters) – rectangular, “flat files” – sequential access n Later the book will discuss – binary files – direct (random) access

Opening a file n To open a file use the OPEN statement. – First

Opening a file n To open a file use the OPEN statement. – First specify the UNIT number of the file – Then the name of the FILE – Other specifiers are • • STATUS (NEW, OLD, REPLACE) ACTION (READ, WRITE, READWRITE) POSITION (REWIND, APPEND, ASIS) IOSTAT (0 = ok, >0 = error)

Examples n n n OPEN (UNIT=12, FILE=“mydata. dat”, STATUS = “OLD”, ACTION=“READ”, POSITION=“REWIND”, IOSTAT=Open.

Examples n n n OPEN (UNIT=12, FILE=“mydata. dat”, STATUS = “OLD”, ACTION=“READ”, POSITION=“REWIND”, IOSTAT=Open. Status) OPEN(12, FILE=“mydat”) Both forms yield the same results due to default settings

Using IOSTAT n n IOSTAT means Input/Output Status If the value is placed in

Using IOSTAT n n IOSTAT means Input/Output Status If the value is placed in a variable it may be used later in the program. OPEN (12, FILE=“mydata. dat”, IOSTAT=Open. Status) IF (Open. Status > 0) STOP “**** Cannot open file ****”

CLOSE n n n Any file that is OPENed should also be closed after

CLOSE n n n Any file that is OPENed should also be closed after your program is done using it. CLOSE (12) Only the UNIT number needs to be specified.

READ, IOSTAT and END= n n n IOSTAT is used with READ to help

READ, IOSTAT and END= n n n IOSTAT is used with READ to help a program stop reading from a file when it encounters the end of that file (eof). READ (12, *, IOSTAT = Input. Status) num 1, num 2 IF (Input. Status < 0) EXIT Another method is READ (12, *, END=20) num 1, num 2 20 PRINT*, “The end of the file has been encountered”