Presentation is loading. Please wait.

Presentation is loading. Please wait.

Input and output. Input streams n there are two ways of handling I/o n list-directed –done using default settings –PRINT*, num –READ*, num n formatted.

Similar presentations


Presentation on theme: "Input and output. Input streams n there are two ways of handling I/o n list-directed –done using default settings –PRINT*, num –READ*, num n formatted."— Presentation transcript:

1 Input and output

2 Input streams n there are two ways of handling I/o n 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

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

4 PRINT statements n List directed –PRINT*, num n Formatted –PRINT ‘(1x, i5, f8.2)’, number, temperature n With format labels –PRINT 10, number, temperature –10 FORMAT (1x, i5, f8.2)

5 Format descriptors n (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. n A format descriptor designated the exact number of characters that will be printed and where they will be printed.

6 The integer format descriptor n 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 -------------> **

7 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 –num = 123.45 –PRINT ‘(f8.2)’, num ---------------> 123.45 –PRINT ‘(f6.2)’, num --------------->123.45 –PRINT*, num ---------------> 123.4500 –PRINT ‘(f3.2)’, num ---------------> ***

8 The character data descriptor n 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

9 The spacing descriptors n X - stands for ‘print a blank space’ n 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

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

11 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

12 Carriage controls n 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

13 Repeating Edit Descriptors n Place a number in front of descriptor nI4repeats I4 n times 3F5.2repeats F5.2 3 times 4(5X,I2)repeats 5X & I2 4 times

14 More descriptors Twtabs over to column w /starts rest of statement on a new line n(/)starts rest of statement in n lines Ew.dreads numbers in scientific notation

15 Literals n Individual characters may also be inserted in formats. –PRINT ‘(“$”, f6.2)’, price ----------> $123.45 –PRINT ‘(10(“-”))’ ---------------> ==========

16 Printing tables n First print the heading n 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

17 READ statements n List directed –READ*, num n Formatted –READ ‘(1x, i5, f8.2)’, number, temperature n With format labels –READ 10, number, temperature –10 FORMAT (1x, i5, f8.2)

18 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 ‘(f6.2)’, num –The data entered may be either with decimal point (123.45) or not ( 12345)

19 The WRITE statement n is used to write to files n Underlies the PRINT statement n PRINT*, is really WRITE (*,*) or WRITE (6,*) n 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’

20 WRITE default examples n WRITE (*, *) num1, num2 n WRITE (*, ‘(i4,1x,i6)’) num1, num2 n WRITE (*, 20) num1, num2 n 20 FORMAT (1x. I4, t12, i5) n WRITE (UNIT = 6, FMT = 30) num1, num2

21 READ default settings n is used to read from files n 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’

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

23 File processing n Data commonly comes to us in files. n 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

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

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

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

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

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


Download ppt "Input and output. Input streams n there are two ways of handling I/o n list-directed –done using default settings –PRINT*, num –READ*, num n formatted."

Similar presentations


Ads by Google