next up previous contents
Next: Editor Descriptors Up: Input and Output. Previous: List-Directed Input and Output   Contents


A console READ with format has the form

read "(editor_descriptor_list)" input_list
(note that the star has been replaced by the actual format, that is, by string containing a list of editor descriptors, between paranthesis). The values for the input_list variables will be read in succesively, in the format specified by the corresponding descriptor from the editor_descriptor_list.

For example, we can replace the READ *, r1, r2, r3 statement by

read "(F2.1,F2.1,F2.1)",r1,r2,r3
The Fw.d descriptor means that the next w characters of the input stream represent a Floating point number; if a decimal point is not present in the data, then the last d characters are the digits of the fractional part.

Therefore, an input line of the form

will be interpreted as follows. The first edit descriptor in the list is F2.1; therefore, the first 2 characters (11) are read in from the input stream, and considered to represent a floating point number; since there is no decimal point in the data, the last character is the fractional part, therefore the value is 1.1 and is assigned to r1. At this time we are done with the first edit descriptor in the list, and we move to the second (also F2.1). The next 2 characters (22) are read in, and similarly, they are resolved to represent the floating point number 2.2, value which is assigned to r2. Similar treatment applies to the last edit descriptor.

Suppose now our input line has the easy-to-read form

1.1, 2.2, 3.3
(we have decimal points, and two consecutive numbers are separated by a comma and a blank). Then, the numbers are read in correctly if we use the following statement
read "(F3.1,2X,F3.1,2X,F3.1)",r1,r2,r3
The first edit descriptor is F3.1; therefore, the first 3 characters (1.1) represent a floating point value, which is assigned to r1; since the data contains a decimal point, we ignore the edit specification of the fractional part. The edit descriptor nX skips the next n characters; therefore, the 2X descriptor will lead to ignoring the comma and the blank. Next edit desciptor, F3.1 will read in 3 characters, 2.2, which represent a real value and which is assigned to r2; etc.

Slashes in the format position the input stream pointer at the beginning of the next record (here, the next line); for example, if we read

read "(F3.1,2X,F3.1,/,F3.1)",r1,r2,r3
and the input from the console is
1.1, 2.2, 3.3 <CR> \\
the assigned values are r1=1.1, r2=2.2, r3=4.4.

The formatted console PRINT has the form

print "(editor_descriptor_list)" output_list
The values of the variables in the output_list will be printed out succesively, each one obeying the format imposed by the corresponding descriptor from the editor_descriptor_list.

We want to print the 3 computed areas; since the radii have just one fractional digit, it is probably sufficient to have the results rounded to 3 fractional digits.

We can use the following statement

PRINT "(A8,F7.3,2X,F7.3,2X,F7.3)","Areas = ",a1,a2,a3
The Aw edit desciptor specifies CHARACTER data of width w. The first object in the output list is the string "Areas = "; the first edit descriptor is A8, therefore the first 8 characters in the output stream will be Areas = . If we want to print the full string argument, without counting for its length, we can use the plain descriptor A (without any width specification).

The second object in the output list is the REAL a1, and it will be printed in a format specified by the second edit descriptor (F7.3; the number will be displayed on 7 characters, with 3 digits for the fractional part (the value of a1 will be rounded to 3 digits after the decimal point); Since there are 7 characters, we use 3 for the fractional part and one for the decimal point, the integer part will be displayed on the remaining 3 characters. The output is

Areas =   3.801   15.205   34.212
Note that if we initialize r1=20. (possible with 3 characters), the area a1=1256.637, a number whose integer part contains 4 digits; this number cannot be represented in the F7.3 format, and the produced output is ******* (7 stars).

In general, it is recommended that we allow a generous width w when printing numbers in the Fw.d format; if we allow for more places than the number needs, the representation will be aligned to the right, with blanks inserted in the leftmost positions (see 3.801 above). It is also recommended that The number of digits of the fractional part should be in tune with the number of accurate digits produced by the computation. For example, if we do the computations in single precision we have at most 7 accurate digits in the results; it is pointless to use a F20.10 format, which would print out 3 extra digits containing ``garbage''.

The 3rd descriptor is 2X; this will print 2 blanks in the output stream. From here on the behavior should be clear: print a2 in the F7.3 format, then insert 2 spaces, then print a3 in the same format.

The Fw.d format prints REAL values in the decimal point notation (integer part, point, decimal part). Alternatively, we can choose to print numbers in the exponent form. For this, we use the descriptor Ew.d, where w is the total number of characters (the width of the representation), and d is the number of decimal places in the normalized number; note that in this case d truly represents the number of digits printed. For example, the statement

print "(a,e10.3,2x,e10.3,2x,e10.3)","areas = ",a1,a2,a3
produces the result
Areas =  0.380E+01   0.152E+02   0.342E+02
Note that the characters ., E, + are counted toward the total specified width of 10 places.

Let us mention that groups of descriptors can be repeated by enclosing the group between parantheses, and prefixing this expression with the number of repeats. For example, the print example above can be written in a shorter form as

print "(a,3(e10.3,2x))","areas = ",a1,a2,a3

The string "Areas = " can be embedded as a constant in the editor descriptor list; the following statement produces the same output as the statement above

print "('Areas = ',3(E10.3,2X))",a1,a2,a3
(note the interplay between double and single quotes in delimiting a string within a string).

Slashes in the format move the output stream pointer to the next record (line).

print "('Areas = ',//,3(F7.3,/))",a1,a2,a3
produces the output
Areas = 


If the number of items in the output_list is greater than the number of edit descriptors in the format, the edit descriptor list is ``wrapped around'' and read again from the beginning. The statement

print "('Areas = ',(F7.3,2X))",a1,a2,a3
produces the output
Areas =   3.801 
Areas =  15.205 
Areas =  34.212

next up previous contents
Next: Editor Descriptors Up: Input and Output. Previous: List-Directed Input and Output   Contents
Adrian Sandu 2001-08-26