CEE15 Introduction to Computing for Civil Engineers 5

CEE15 Introduction to Computing for Civil Engineers 5 -...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
CEE15 Introduction to Computing for Civil Engineers 42 Chapter 5 Input/Output and File Processing 5.1 Formatted Output Fortran formats are used to control the appearance of the input and output. 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(*,*) Examples: READ(*,'(2I5,F10.2)') ... variables . .. READ(*,"(5F10.2)") ... variables . .. WRITE(*,'(A,I5)') ... variable and expressions . .. WRITE(*,"(10F5.2)") ... variable and expressions . .. Since a format is a character string, we can declare a character constant to hold a format string Examples: CHARACTER(LEN=20), PARAMETER :: FMT1 = "(I5,F10.2)" CHARACTER(LEN=*), PARAMETER :: FMT2 = "(4I5, 5E14.7, 8F5.0)" READ(*,FMT1) ... variables . .. READ(*,FMT1) ... variables . .. WRITE(*,FMT2) . .. variables and expressions . .. WRITE(*,FMT2) . .. variables and expressions . .. In the above, character constants (defined as PARAMETER) 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. CHARACTER(LEN=80) :: String String = "(3I5, 10F8.2)" READ(*,String) ... variables . .. WRITE(*,String) . .. variables and expressions . .. Note that the same format can be used in both READ and WRITE statements. 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. CHARACTER(LEN=10) :: FMT FMT = "(I2,F3.5,E15.7)" WRITE(*,FMT) . ..... Since FMT has length 10 and the format contains 15 characters, what FMT can actually have is (I2,F3.5,E) which is not a complete format. IMPORTANT: You can use both listed-directed and formatted READs and WRITEs in your program.
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
CEE15 Introduction to Computing for Civil Engineers 43 5.1.1 INTEGER Output: The I Descriptor The I w and I w.m descriptors are for INTEGER output. The general form of these descriptors is as follows: r I w and r I w.m The meaning of I, w and m are: I is for INTEGER w is the width of field , which indicates that an integer should be printed with w positions. m indicates that at least m positions (of the w positions) must contain digits. If the number to be printed has fewer than m digits, leading 0s are filled. If the number has more than m digits, m is ignored and in this case I w.m is equivalent to I w . Note:
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 17

CEE15 Introduction to Computing for Civil Engineers 5 -...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online