280 likes | 411 Views
Input and output. Input streams. there are two ways of handling I/o list-directed done using default settings PRINT*, num READ*, num formatted controlled by the programmer you can specify every detail of what is printed. Output statements. PRINT only used for output to the screen
E N D
Input streams • there are two ways of handling I/o • list-directed • done using default settings • PRINT*, num • READ*, num • formatted • controlled by the programmer • you can specify every detail of what is printed
Output statements • PRINT • only used for output to the screen • WRITE • used for output to files
PRINT statements • List directed • PRINT*, num • Formatted • PRINT ‘(1x, i5, f8.2)’, number, temperature • With format labels • PRINT 10, number, temperature • 10 FORMAT (1x, i5, f8.2)
Format descriptors • (1x, i5, f8.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. • A format descriptor designated the exact number of characters that will be printed and where they will be printed.
The integer format descriptor • i used for integers, can be repeated (5i) and can have width specified (i5) • INTEGER :: num • num = 123 • PRINT ‘(i3)’, num -------------> 123 • PRINT ‘(3i)’, num -------------> 123123123 • PRINT *, num -------------> 123 • PRINT ‘(i2)’, num -------------> **
The real number descriptor • F for ‘floating point’ (real) numbers. • Followed by an integer total field width, ‘.’, then an integer number of decimal digits. • REAL :: num • num = 123.45 • PRINT ‘(f8.2)’, num ---------------> 123.45 • PRINT ‘(f6.2)’ , num --------------->123.45 • PRINT*, num ---------------> 123.4500 • PRINT ‘(f3.2)’, num ---------------> ***
The character data descriptor • a used for ‘alpha-numeric data’ • CHARACTER(10) :: name • name = “Washington” • PRINT ‘(a10)’, name ---------------> Washington • PRINT ‘(a15)’, name ---------------> Washington_____ • PRINT*, name ---------------> Washington • PRINT ‘(a5)’ ---------------> Washi
The spacing descriptors • X - stands for ‘print a blank space’ • T - stands for ‘print a tab’ • INTEGER :: num1, num2 • num1 = 123 • num2 = 456 • PRINT ‘(1x,i5,5x,i3)’, num1, num2 • ___123_____456 • PRINT ‘(t5, i3), num1 ---------------> _____123
Difference between x and t • 5x gives five spaces • t5 tabs out to column 5 (gives only 4 spaces!) • When used in tables together they are easy to confuse.
Repetition and width nX if first, specifies line spacing nX 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 • FIRST EDIT DESCRIPTOR • ‘ ‘ OR 1X 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 • Place a number in front of descriptor nI4 repeats I4 n times 3F5.2 repeats F5.2 3 times 4(5X,I2) repeats 5X & I2 4 times
More descriptors Tw tabs over to column w / starts rest of statement on a new line n(/) starts rest of statement in n lines Ew.d reads numbers in scientific notation
Literals • Individual characters may also be inserted in formats. • PRINT ‘(“$”, f6.2)’, price ----------> $123.45 • PRINT ‘(10(“-”))’ ---------------> ==========
Printing tables • First print the heading • Then a loop fills in the body of the table • PRINT ‘(t10, “Number”, t20, “Square”)’ • DO n=1, 5 • PRINT ‘(9x, i6, t20, i6)’, n, n**2 • ENDDO • ---------Number----Square • --------- 1---- 1 • --------- 2---- 4 • --------- 3---- 9 • --------- 4---- 16 • --------- 5---- 25
READ statements • List directed • READ*, num • Formatted • READ ‘(1x, i5, f8.2)’, number, temperature • With format labels • READ 10, number, temperature • 10 FORMAT (1x, i5, f8.2)
Reading real numbers • 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 ‘(f6.2)’, num • The data entered may be either with decimal point (123.45) or not ( 12345)
The WRITE statement • 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 • WRITE (*, *) num1, num2 • WRITE (*, ‘(i4,1x,i6)’) num1, num2 • WRITE (*, 20) num1, num2 • 20 FORMAT (1x. I4, t12, i5) • WRITE (UNIT = 6, FMT = 30) num1, num2
READ default settings • 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 • 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 • Data commonly comes to us in files. • We will learn how to handle • text files (ASCII characters) • rectangular, “flat files” • sequential access • Later the book will discuss • binary files • direct (random) access
Opening a file • 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 • OPEN (UNIT=12, FILE=“mydata.dat”, STATUS = “OLD”, ACTION=“READ”, POSITION=“REWIND”, IOSTAT=OpenStatus) • OPEN(12,FILE=“mydat”) • Both forms yield the same results due to default settings
Using IOSTAT • 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=OpenStatus) • IF (OpenStatus > 0) STOP “**** Cannot open file ****”
CLOSE • 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= • 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 = InputStatus) num1, num2 • IF (InputStatus < 0) EXIT • Another method is READ (12,*, END=20) num1, num2 • 20 PRINT*, “The end of the file has been encountered”