Sie sind auf Seite 1von 3

Fortran Formats

We have discussed the READ and WRITE statements. These are the so-called list-directed input/output statements. They are also referred to as free-format input/output statements. List-directed input/output statements are easy to use; however, we have no control over the appearance of the input and output. To overcome this problem, we should use formats. Fortran formats are used to control the appearance of the input and output. It has the following simple form:
(...fra ei dsrpos...) .. omt dt ecitr ..

That is, a Fortran format is a pair of parenthesis that contains format edit descriptors separated by commas. There are three possible ways to prepare a Fortran format. Fortran has a FORMAT statement; but, we will not use it because the two methods discussed below offer higher level of flexibility. Write the format as a character string and use it to replace the second asterisk in READ(*,*) or WRITE(*,*).
RA(,(I,1.)) ..vrals.. ED*'25F02' . aibe . RA(,(F02" ED*"51.)) ..vrals.. . aibe . WIE*'AI)) RT(,(,5' ..vral adepesos.. . aibe n xrsin . WIE*"1F.)) ..vral adepesos.. RT(,(052" . aibe n xrsin .

The above has two READ and two WRITE statements whose second asterisks are replaced with format character strings. 2I5, F10.2, 5F10.2, A,I5 and 10F5.2 are format edit descriptors. Since a format is a character string, we can declare a character constant to hold a format string.
CAATRLN2) PRMTR: FT ="I,1.) HRCE(E=0, AAEE : M1 (5F02" CAATRLN*,PRMTR : FT ="45 51.,850" HRCE(E=) AAEE : M2 (I, E47 F.) RA(,M1 ..vrals.. ED*FT) . aibe . RA(,M1 ..vrals.. ED*FT) . aibe . WIE*FT)..vralsadepesos.. RT(,M2 . aibe n xrsin . WIE*FT)..vralsadepesos.. RT(,M2 . aibe n xrsin .

In the above, character constants (defined as PARAMETERs) FMT1 and FMT2 are used as formats. We can also use a character variable to hold a format. In the example below, the character variable String is set to a format and used in READ and WRITE statements.
CAATRLN8): Srn HRCE(E=0 : tig Srn ="35 1F.) tig (I, 082" RA(,tig ..vrals.. ED*Srn) . aibe . WIE*Srn)..vralsadepesos.. RT(,tig . aibe n xrsin .

Note that the same format can be used in both READ and WRITE statements.
www.cs.mtu.edu/~shene/COURSES/cs201/NOTES/chap05/format.html 1/3

WARNING: The length of the string which contains a format must be large enough. Otherwise, the format stored there becomes incomplete and causes format error. Consider the following example.
CAATRLN1): FT HRCE(E=0 : M FT="I,35E57" M (2F.,1.) WIE*FT ... RT(,M) ...

Since FMT has length 10 and the format contains 15 characters, what FMT can actually have is
(2F., I,35E

which is not a complete format.

Format Edit Descriptors


The tedious part of using Fortran format is to master many format edit descriptors. Each edit descriptor tells the system how to handle certain type of values or activity. Each value requires some positions. For example, an integer of four digits requires at least four positions to print. Therefore, the number of positions to be used is the most important information in an edit descriptor. We shall use the following convention of symbols: w: the number of positions to be used m: the minimum number of positions to be used d: the number of digits to the right of the decimal point e: the number of digits in the exponent part Although we may print a number using as many positions as you want, this is only for input/output. This number of positions is not the precision (i.e., the number of significant digits) of that number. To be more precisely, computers normally can store real numbers up to seven significant digits. This is the precision of real numbers. However, we can print a real number using 50 positions in which 25 positions are for the fraction part. This is only a way of describing the appearance and does not change the precision of real numbers. The following are the editor descriptors to be discussed. Details will be given on subsequent pages. Purpose Reading/writing INTEGERs Decimal form Reading/writing REALs Exponential form Scientific form Engineering form Reading/writing LOGICALs
www.cs.mtu.edu/~shene/COURSES/cs201/NOTES/chap05/format.html

Edit Descriptors Iw Iw.m Fw.d Ew.d ESw.d ENw.d Ew.dEe ESw.dEe ENw.dEe Lw A Aw
2/3

Reading/writing CHARACTERs

Reading/writing CHARACTERs Horizontal Positioning Tabbing Vertical Grouping Others Format Scanning Control Sign Control Blank Control

A nX Tc

Aw

TLc and TRc / r(....) :

S, SP and SS BN and BZ

Most edit descriptors can be repeated and several edit descriptors can be grouped into a group. For most of the cases, edit descriptors are separated by commas. The following is an example:
CAATRLN3): Fra HRCE(E=0 : omt Fra ="5,I.,F03 A E1.) omt (X 52 1., , S47" RA(,omt ..vrals.. ED*Fra) . aibe . WIE*Fra)..vralsadepesos.. RT(,omt . aibe n xrsin .

In the above example, format Format has five edit descriptors 5X, I5.2, F10.3, A and ES14.7. Adjacent edit descriptors are separated by a comma. IMPORTANT: You can use both listed-directed and formatted READs and WRITEs in your program.

www.cs.mtu.edu/~shene/COURSES/cs201/NOTES/chap05/format.html

3/3

Das könnte Ihnen auch gefallen