chapter 3 input/output list-directed (free formatted) input/output as in section 2.6 formatted...

12
CHAPTER 3 INPUT/OUTPUT List-directed (free formatted) input/output As in section 2.6 Formatted input/output General Form: Read format-specifier, input-list

Post on 21-Dec-2015

227 views

Category:

Documents


5 download

TRANSCRIPT

Page 1: CHAPTER 3 INPUT/OUTPUT List-directed (free formatted) input/output As in section 2.6 Formatted input/output General Form: Read format-specifier, input-list

CHAPTER 3INPUT/OUTPUT

•List-directed (free formatted) input/outputAs in section 2.6

•Formatted input/output General Form: Read format-specifier, input-list

Page 2: CHAPTER 3 INPUT/OUTPUT List-directed (free formatted) input/output As in section 2.6 Formatted input/output General Form: Read format-specifier, input-list

• 1. * (an asterisk)It indicates list-directed input (free format).Example: read *,a,j

• 2. A character constant or a character variable (or expression or array) whose value specifies the format for the input.Example: read ‘(I3,I4,I7)’,j,k,l

• 3. The label of a FORMAT statement

label FORMAT (list of format descriptors)

Page 3: CHAPTER 3 INPUT/OUTPUT List-directed (free formatted) input/output As in section 2.6 Formatted input/output General Form: Read format-specifier, input-list

Descriptors

• Example:Read 10, I, r, t 10 format (2x, i5, f7.3, f6.2)

Page 4: CHAPTER 3 INPUT/OUTPUT List-directed (free formatted) input/output As in section 2.6 Formatted input/output General Form: Read format-specifier, input-list

• X- DescriptorIt skips positions on the data line

General Form: nX • I- Descriptor

It is used when reading a value into an integer variable.

General Form: nIww : width of the field and n is the repetition indicator.

• Example:read 10,m,n

10 format(5x,i5,i7)

Page 5: CHAPTER 3 INPUT/OUTPUT List-directed (free formatted) input/output As in section 2.6 Formatted input/output General Form: Read format-specifier, input-list

• F- DescriptorIt is used when reading a value into a real variable.

General Form: Fw.dw: total number of positions to use from the data line and d: number of decimal positions.

• REMARKS:• If there is a decimal point included in the w

positions, the value will be stored as it was entered, regardless of what value has been given to d.

• If there is no decimal point within the specified positions, the value of d is used to position a decimal place before storing the value. If the characters 1246 are read using F4.1, the value stored is 124.6.

Page 6: CHAPTER 3 INPUT/OUTPUT List-directed (free formatted) input/output As in section 2.6 Formatted input/output General Form: Read format-specifier, input-list

• Example: Read 10, I, r, t

10 format (2x, i5, f7.3, f6.2)

• Example: read 50,a,c,k

50 format(2x,2f5.1,2x,i4)• For: a=80.1, c=36.5 and k = 40, the input line

should be as:

8 0 . 1 3 6 . 5 4 0

2x 2f5.1 2x I4

Page 7: CHAPTER 3 INPUT/OUTPUT List-directed (free formatted) input/output As in section 2.6 Formatted input/output General Form: Read format-specifier, input-list

• E- Descriptorused to read a variable entered in an exponential form.

General Form: Ew.dW: total number of positions to use from the data line and d: the number of decimal positions.

• Example:read 30,Iter,error

30 format(2x,i5,2x,e7.2)• Data line, Iter=12 and error=0.25e-09:

1 2 . 2 5 E - 0 9

2x I5 2x E7.2

Page 8: CHAPTER 3 INPUT/OUTPUT List-directed (free formatted) input/output As in section 2.6 Formatted input/output General Form: Read format-specifier, input-list

• A-Descriptorused to read character variable.General Form: AwWhere w represents the number of positions to be read.

Page 9: CHAPTER 3 INPUT/OUTPUT List-directed (free formatted) input/output As in section 2.6 Formatted input/output General Form: Read format-specifier, input-list

Output Formatting

• Similar to input formatting

Only one difference Carriage Character Control Single space used to control output

Carriage Character Control characters to be printed

Page 10: CHAPTER 3 INPUT/OUTPUT List-directed (free formatted) input/output As in section 2.6 Formatted input/output General Form: Read format-specifier, input-list

• Example:Print 4

4 Format(‘Fortran course')

• The buffer output line:

• And the output is:F o r t r a n c o u r s e

o r t r a n c o u r s e

• Example:Print 4

4 Format(1x‘Fortran course')

• The buffer output line:

• And the output is:F o r t r a n Co u r s e

F o r t r a n c o u r s e

Page 11: CHAPTER 3 INPUT/OUTPUT List-directed (free formatted) input/output As in section 2.6 Formatted input/output General Form: Read format-specifier, input-list

More Examples• Example 1:

Given: K=20, L=-30, M=-450Print 4, K,L,M4 Format(1X,I3,2X,I2,3X,I4)

• Example 2:Given: T=-0.000234, R= 100.68Print 4, T,R4 Format(1X,E10.2,3X,F7.3)

Page 12: CHAPTER 3 INPUT/OUTPUT List-directed (free formatted) input/output As in section 2.6 Formatted input/output General Form: Read format-specifier, input-list