1 / 28

Input and output

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

zinna
Download Presentation

Input and output

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Input and output

  2. 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

  3. Output statements • PRINT • only used for output to the screen • WRITE • used for output to files

  4. 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)

  5. 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.

  6. 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 -------------> **

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

  8. 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

  9. 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

  10. 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.

  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 • 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 • 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

  14. 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

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

  16. 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

  17. 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)

  18. 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)

  19. 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’

  20. 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

  21. 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’

  22. 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.

  23. 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

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

  25. 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

  26. 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 ****”

  27. 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.

  28. 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”

More Related