14
Chapter 5 Basic I/O Concepts Dr. Ali Can Takinacı İstanbul Technical University Faculty of Naval Architecture and Ocean Engineering İstanbul - Turkey Tel: +90 (212 285 6519) Fax: +90 (212 285 6508) E-mail: [email protected]

Chapter 5 Basic I/O Concepts Dr. Ali Can Takinacı İstanbul Technical University Faculty of Naval Architecture and Ocean Engineering İstanbul - Turkey Tel:

  • View
    218

  • Download
    3

Embed Size (px)

Citation preview

Page 1: Chapter 5 Basic I/O Concepts Dr. Ali Can Takinacı İstanbul Technical University Faculty of Naval Architecture and Ocean Engineering İstanbul - Turkey Tel:

Chapter 5 Basic I/O Concepts

Dr. Ali Can Takinacıİstanbul Technical University

Faculty of Naval Architecture and Ocean Engineeringİstanbul - Turkey

Tel: +90 (212 285 6519) Fax: +90 (212 285 6508) E-mail: [email protected]

Page 2: Chapter 5 Basic I/O Concepts Dr. Ali Can Takinacı İstanbul Technical University Faculty of Naval Architecture and Ocean Engineering İstanbul - Turkey Tel:

Basic I/O Concepts

•In the previous chapters, we have read values into and written them out of our programs using list-directed READ and WR IT E statements. •List-directed I/O statements are said to be in free format. •Free format is specified by the second asterisk in the READ( *,*) and WRITE (*, *) statements. •As we saw, the results of writing out data in free format are not always pretty. •There are often a large number of extra spaces in the output. •In this chapter, we will learn how to write out data by using formats that specify the exact way in which the numbers should be printed out.

Basic I/O Concepts

Page 3: Chapter 5 Basic I/O Concepts Dr. Ali Can Takinacı İstanbul Technical University Faculty of Naval Architecture and Ocean Engineering İstanbul - Turkey Tel:

FORMATS AND FORMATTED WRITE

STATEMENTS

•A format may be used to specify the exact manner in which variables are to be printed out by a program.• In general, a format can specify both the horizontal and the vertical position of the variables on the paper, and also the number of significant digits to be printed out. •A typical formatted WRITE statement for an integer i and a real variable resul t is shown below:

© 2010, Dr. ALİ CAN TAKİNACI Slide No: 3

Label of statement format descriptors

Basic I/O Concepts

Page 4: Chapter 5 Basic I/O Concepts Dr. Ali Can Takinacı İstanbul Technical University Faculty of Naval Architecture and Ocean Engineering İstanbul - Turkey Tel:

• In addition to FORM AT statements, formats may be specified in character constants or variables. If a character constant or variable is used to contain the format, then the constant or the name of the variable appears within the parentheses in the WRITE statement.

• For example, the following three WRITE statements are equivalent:

Basic I/O Concepts

Page 5: Chapter 5 Basic I/O Concepts Dr. Ali Can Takinacı İstanbul Technical University Faculty of Naval Architecture and Ocean Engineering İstanbul - Turkey Tel:

FORMAT DESCRIPTORS

There are many different format descriptors. They fall into four basic categories: 1. Format descriptors that describe the vertical position of a line of text. 2. Format descriptors that describe the horizontal position of data in a line. 3. Format descriptors that describe the output format of a particular value. 4. Format descriptors that control the repetition of portions of a format.

Basic I/O Concepts

Page 6: Chapter 5 Basic I/O Concepts Dr. Ali Can Takinacı İstanbul Technical University Faculty of Naval Architecture and Ocean Engineering İstanbul - Turkey Tel:

Real Output-The F Descriptor

•One format descriptor used to describe the display format of real data is the F descriptor. •It has the form

• rFw.d

•Real values are printed right justified within their fields. •If necessary, the number will be rounded off before it is displayed

Basic I/O Concepts

Page 7: Chapter 5 Basic I/O Concepts Dr. Ali Can Takinacı İstanbul Technical University Faculty of Naval Architecture and Ocean Engineering İstanbul - Turkey Tel:

Basic I/O Concepts

Real Output-The E Descriptor

•Real data can also be printed in exponential notation using the E descriptor. •Scientific notation is a popular way for scientists and engineers to display very large or very small numbers. •It has the form

• rEw.d

•For example, suppose that we want to print out a variable in the E format with •4 significant digits of accuracy. •Then a field width of 11 characters is required, as shown below: 1 for the sign of the mantissa, 2 for the zero and decimal point, 4 for the actual mantissa, 1 for the E, 1 for the sign of the exponent, and 2 for the exponent itself.

© 2010, Dr. ALİ CAN TAKİNACI Slide No: 7

the width of an E format descriptor field

Basic I/O Concepts

Page 8: Chapter 5 Basic I/O Concepts Dr. Ali Can Takinacı İstanbul Technical University Faculty of Naval Architecture and Ocean Engineering İstanbul - Turkey Tel:

• Notice that the fourth field is all asterisks, since the format descriptor does not satisfy Equation w >=d+7

© 2010, Dr. ALİ CAN TAKİNACI Slide No: 8Basic I/O Concepts

Page 9: Chapter 5 Basic I/O Concepts Dr. Ali Can Takinacı İstanbul Technical University Faculty of Naval Architecture and Ocean Engineering İstanbul - Turkey Tel:

Logical Output-The L Descriptor

•The descriptor used to display logical data has the form

• rLw

•The value of a logical variable can only be .TRUE. or .FALSE.• The output of a logical variable is either a T or an F, right justified in the output field.

© 2010, Dr. ALİ CAN TAKİNACI Slide No: 9Basic I/O Concepts

Page 10: Chapter 5 Basic I/O Concepts Dr. Ali Can Takinacı İstanbul Technical University Faculty of Naval Architecture and Ocean Engineering İstanbul - Turkey Tel:

Character Output-The A Descriptor

•Character data is displayed by using the A format descriptor.

• rA or rA.w

© 2010, Dr. ALİ CAN TAKİNACI Slide No: 10Basic I/O Concepts

Page 11: Chapter 5 Basic I/O Concepts Dr. Ali Can Takinacı İstanbul Technical University Faculty of Naval Architecture and Ocean Engineering İstanbul - Turkey Tel:

Horizontal Positioning-The X and T Descriptors

•The X descriptor has the form

• nX

•where n is the number of blanks to insert. It is used to add one or more blanks between two values on the output line.

•The T descriptor has the form

• Tc

•where c is the column number to go to.•It is used to jump directly to a specific column in the output buffer. •The T descriptor works much like a "tab" character on a typewriter, except that it is possible to jump to any position in the output line, even if we are already past that position in the FORMAT statement

© 2010, Dr. ALİ CAN TAKİNACI Slide No: 11Basic I/O Concepts

Page 12: Chapter 5 Basic I/O Concepts Dr. Ali Can Takinacı İstanbul Technical University Faculty of Naval Architecture and Ocean Engineering İstanbul - Turkey Tel:

The course name begins in column 51 of the buffer, but it is printed in column 50, since the first character in the output buffer is the control character.)

© 2010, Dr. ALİ CAN TAKİNACI Slide No: 12Basic I/O Concepts

Page 13: Chapter 5 Basic I/O Concepts Dr. Ali Can Takinacı İstanbul Technical University Faculty of Naval Architecture and Ocean Engineering İstanbul - Turkey Tel:

Repeating Groups of Format Descriptors

•We have seen that many individual format descriptors can be repeated by preceding them with a repeat count. •For example, the format descriptor 2I10 is the same as the pair of descriptors I10, I10.

© 2010, Dr. ALİ CAN TAKİNACI Slide No: 13Basic I/O Concepts

Page 14: Chapter 5 Basic I/O Concepts Dr. Ali Can Takinacı İstanbul Technical University Faculty of Naval Architecture and Ocean Engineering İstanbul - Turkey Tel:

Changing Output Lines-The Slash (/) Descriptor

•The slash (/) descriptor causes the current output buffer (line) to be sent to the printer, and a new output buffer to be started.

© 2010, Dr. ALİ CAN TAKİNACI Slide No: 14Basic I/O Concepts