Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Get to know Fortran - Chapter 4 - Fortran I/O

Get to know Fortran - Chapter 4 - Fortran I/O

A quick introduction to Fortran Input/Output syntax and semantics

Roger Ferrer Ibanez

December 29, 2012
Tweet

Other Decks in Programming

Transcript

  1. Programming Models 2 Quick recap of last chapter • We

    learnt to use data • Scalars • Arrays – explicit shape, deferred shape, assumed size, assumed shape – array sections, array-indexed arrays – array-wise operations • Allocatable • Pointers
  2. Programming Models 3 Input/Output in Fortran • Input/Output in Fortran

    looks rusty – 50 years of language evolution have not done any good • We will see IO in three parts – Concepts – Statements – FORMAT edition
  3. Programming Models 5 Fortran files • Fortran performs input/output (IO)

    on files • There are two kinds of files in Fortran – external files – internal files • An external file is the way a Fortran program can communicate with external entities • An internal file is a string of the Fortran program itself on which you can do IO
  4. Programming Models 6 Fortran records (1) • A file in

    Fortran is a sequence of records – These records are logical, they do not have to exist in the file • There are three kinds of records – Formatted – Unformatted – Endfile 1 "ABCD" 1.234e34 "FOO" 24.234 Formatted records Record 1 Record 2 Unformatted records 0x43 0x44 0x42 0x41 0x04
  5. Programming Models 7 Fortran records (2) • A formatted record

    is a character sequence that represents some data – Its length is measured in characters – They are read or written only through formatted IO statements • An unformatted record is a platform dependent representation of data – Its length is measured in processor dependent units (99% times the processor dependent unit will be a byte)
  6. Programming Models 8 Fortran records (3) • Endfile record –

    Marks that a file ends – Remember that it does not have to physically exist
  7. Programming Models 9 External files attributes • An external file

    has several attributes – Access – Position – Existence
  8. Programming Models 10 File access (1) • There are two

    (three in Fortran 2003) ways to access the records of an external file – Sequential – Direct – Stream (Fortran 2003)
  9. Programming Models 11 File access (2) • Sequential – Each

    record is read/written after the other – All records are either formatted or unformatted • Direct – Access records of the external file in any order – Records cannot be deleted and have an immutable record number set the first time they are written – All records are either formatted or unformatted – All records must have the same length • Stream – Like direct but instead of records you work on processor dependent units (≈ bytes)
  10. Programming Models 12 File position • Execution of IO statements

    may change the position attribute of an external file – initial point: right before the first record of the external file – terminal point: right after the last record (it may or may not be an endfile record) – There is a current record only if we are inside a record • Advancing IO moves to the next record in the file • Nonadvancing IO stays in the current record
  11. Programming Models 13 File existence • File existence is the

    property of an external file to be processable in some way by a Fortran program • An external file may not exist but be preconnected to a Fortran program – such file will exist after the first IO performed on it – e.g. standard input or output • If you do not print/read anything from these, they do not exist in the Fortran program
  12. Programming Models 14 Units and file connection • A unit

    is the Fortran way to refer to a file – external units refer to external files – internal units refer to internal files • External units are identified by a nonnegative integer – This number is global among all program units • Special unit called * (asterisk) – * are platform dependent external units preconnected for formatted sequential access. There can be more than one but usually is just stdout. • Internal units are identified by a CHARACTER variable
  13. Programming Models 16 Connecting to a file (1) • To

    connect to a file use OPEN statement OPEN (connection-specifier-list) OPEN (connection-specifier-list) PROGRAM P OPEN(UNIT=12, & FILE="FOO.TXT", & STATUS="NEW" & ) END PROGRAM P PROGRAM P OPEN(UNIT=12, & FILE="FOO.TXT", & STATUS="NEW" & ) END PROGRAM P Use any number but in gfortran avoid unit numbers 0 (stderr), 5 (stdin), and 6 (stdout) Bring an (external) file with name FOO.TXT to existence and connect it to (external) file unit 12 Name of the file It will create a new file. The file must not exist before.
  14. Programming Models 17 Connecting to a file (2) Connect specifier

    Meaning Connect specifier Meaning [UNIT=]unit-expr Unit expression. Either an integer value for external files or a character variable for internal files. Must be the first spec if UNIT= is omitted FORM=char-expr One of "FORMATTED" or "UNFORMATTED". By default is "FORMATTED" if ACCESS="SEQUENTIAL", otherwise "UNFORMATTED" IOSTAT=int-var Variable to keep result of IO RECL=int-expr Length of record in DIRECT access or maximum record size in SEQUENTIAL ERR=label Label to jump if error BLANK=char-expr Treatment of blanks in formatted records. Can be "NULL" or "ZERO" FILE=char-expr Name of the external file ACTION=char-expr One of "READ", "WRITE or "READWRITE" STATUS=char-expr One of "OLD", "NEW", "SCRATCH", "REPLACE" or "UNKNOWN" POSITION=char-expr One of "ASIS", "REWIND" or "APPEND". REWIND moves to initial point, "APPEND" moves just before the endfile record. "ASIS" leaves position unspecified. ACCESS=char-expr One of "SEQUENTIAL", "DIRECT" (or "STREAM") DELIM=char-expr One of "APOSTROPHE", "QUOTE" or "NONE" PAD=char-expr One of "YES" or "NO"
  15. Programming Models 18 Connecting to a file (3) PROGRAM P1

    OPEN (UNIT=10, FILE="FOO.TXT", FORM="FORMATTED") WRITE (UNIT=10, FMT="(A,I12)") "HELLO", 42 CLOSE (UNIT=10) END PROGRAM P1 PROGRAM P1 OPEN (UNIT=10, FILE="FOO.TXT", FORM="FORMATTED") WRITE (UNIT=10, FMT="(A,I12)") "HELLO", 42 CLOSE (UNIT=10) END PROGRAM P1 $ cat FOO.TXT HELLO 42 $ cat FOO.TXT HELLO 42 PROGRAM P1 OPEN (UNIT=10, FILE="FOO.TXT", FORM="UNFORMATTED") WRITE (UNIT=10") "HELLO", 42 CLOSE (UNIT=10) END PROGRAM P1 PROGRAM P1 OPEN (UNIT=10, FILE="FOO.TXT", FORM="UNFORMATTED") WRITE (UNIT=10") "HELLO", 42 CLOSE (UNIT=10) END PROGRAM P1 $ od -t c FOO.TXT 0000000 \t \0 \0 \0 H E L L O * \0 \0 \0 \t \0 \0 0000020 \0 0000021 $ od -t c FOO.TXT 0000000 \t \0 \0 \0 H E L L O * \0 \0 \0 \t \0 \0 0000020 \0 0000021 Formatted IO Unformatted IO 42
  16. Programming Models 19 Disconnect a file (1) • To close

    an external file use CLOSE statement CLOSE (close-specifier-list) CLOSE (close-specifier-list) Specifier Meaning [UNIT=]int-expr External file unit. It must be the first spec if UNIT= is omitted. IOSTAT=int-variable Variable to keep the result of IO ERR=label Label to jump on error STATUS=char-expr One of "KEEP" to keep the file once closed or "DELETE" to remove it. By default "KEEP" unless the file was OPEN with STATUS="SCRATCH" that it will be "DELETE"
  17. Programming Models 20 Disconect a file (2) • A disconnected

    file unit cannot be used later on IO statements that require a connected file unit – Unless we reconnect it through another OPEN
  18. Programming Models 21 CLOSE example PROGRAM P ! FILE= is

    not allowed on SCRATCH OPEN(UNIT=12, STATUS="SCRATCH") WRITE (UNIT=12, FMT="(A)", & "Look ma, Fortran rocks, no calls to tmpfile or similar!" CLOSE(UNIT=12) END PROGRAM P PROGRAM P ! FILE= is not allowed on SCRATCH OPEN(UNIT=12, STATUS="SCRATCH") WRITE (UNIT=12, FMT="(A)", & "Look ma, Fortran rocks, no calls to tmpfile or similar!" CLOSE(UNIT=12) END PROGRAM P Fortran way to have a temporal file PROGRAM P OPEN(UNIT=12, FILE="UNSURE.TXT") WRITE (UNIT=12, FMT="(A)", & "This file will autodestroy itself in a few moments" CLOSE(UNIT=12, STATUS="DELETE") END PROGRAM P PROGRAM P OPEN(UNIT=12, FILE="UNSURE.TXT") WRITE (UNIT=12, FMT="(A)", & "This file will autodestroy itself in a few moments" CLOSE(UNIT=12, STATUS="DELETE") END PROGRAM P Deletes the file when closing If STATUS="DELETE" is omitted then the file would have been "KEEP"
  19. Programming Models 22 Data transfer statements • There are three

    data transfer statements in Fortran • Output data transfer statements – PRINT – WRITE • Input data transfer statements – READ READ ( io-control-spec-list ) [input-item-list] READ format [, input-item-list] READ ( io-control-spec-list ) [input-item-list] READ format [, input-item-list] PRINT format [ , output-item-list ] PRINT format [ , output-item-list ] WRITE ( io-control-spec-list ) [ output-item-list ] WRITE ( io-control-spec-list ) [ output-item-list ] If no UNIT= appears (like in PRINT or in the second form of READ) then it is assumed to be UNIT=*
  20. Programming Models 23 IO control specifiers Specifier Meaning Specifier Meaning

    [UNIT=]io-unit UNIT where the transfer happens. It must be the first spec if UNIT= is omitted ERR=label Label to jump on error [FMT=]format Format of the transfer. It must be the second spec if FMT= is omitted END=label Label to jump on end of file [NML=]namelist-name Format of the transfer according to a NAMELIST name. Must be the second spec if NML= is omitted. Incompatible with FMT= ADVANCE=char-expr One of "YES" or "NO". Defined if the IO is advancing or nonadvancing REC=int-expr Number of records to be read/written SIZE=int-var Number of characters transferred when ADVANCE="NO" IOSTAT=int-var Result of the IO EOR=label Label to jump on end of record
  21. Programming Models 24 Understand the syntax • Since UNIT=, FMT=

    and NML= can be omitted sometimes is hard to understand the syntax READ (*, *) A, B ➔ READ (UNIT=6, FMT=*) A, B READ (6, 101) A, B ➔ READ (UNIT=6, FMT=101) A, B READ (*, "(A,I6)") A, B ➔ READ (UNIT=*, FMT="(A,I6)") A, B READ (*, *) A, B ➔ READ (UNIT=6, FMT=*) A, B READ (6, 101) A, B ➔ READ (UNIT=6, FMT=101) A, B READ (*, "(A,I6)") A, B ➔ READ (UNIT=*, FMT="(A,I6)") A, B INTEGER :: UNIT_NUMBER CHARACTER(LEN=12) :: STR READ (UNIT_NUMBER, LEN) A, B ➔ READ (UNIT = UNIT_NUMBER, FMT=STR) A, B INTEGER :: UNIT_NUMBER CHARACTER(LEN=12) :: STR READ (UNIT_NUMBER, LEN) A, B ➔ READ (UNIT = UNIT_NUMBER, FMT=STR) A, B INTEGER :: UNIT_NUMBER NAMELIST / N / ... ! Soon we will see what is a namelist READ (UNIT_NUMBER, N) A, B ➔ READ (UNIT = UNIT_NUMBER, NML=N) A, B INTEGER :: UNIT_NUMBER NAMELIST / N / ... ! Soon we will see what is a namelist READ (UNIT_NUMBER, N) A, B ➔ READ (UNIT = UNIT_NUMBER, NML=N) A, B My advice: do not omit any of these in newly written code
  22. Programming Models 25 Formatted/Unformatted • Data transfer is formatted if

    a FMT= or a NML= appear in the data transfer statement – if both omitted, then it is unformatted • Syntax of FMT is braindamaging, leave it for later • NML must mention a NAMELIST name which is an easier way (but rarely used) to do formatted transfers in Fortran – This is a Fortran 90 feature
  23. Programming Models 26 Namelist formatting (1) • In the declaration

    part of a program unit one can declare a NAMELIST PROGRAM P IMPLICIT NONE INTEGER :: N REAL :: R CHARACTER(LEN=10) :: C10 NAMELIST /MY_NML/ N, R, C10 N = 42; R = 1.234; C10 = "HELLO" OPEN (UNIT=12, FILE="FOO.TXT") WRITE (UNIT=12, NML=MY_NML) CLOSE(UNIT=12) END PROGRAM P PROGRAM P IMPLICIT NONE INTEGER :: N REAL :: R CHARACTER(LEN=10) :: C10 NAMELIST /MY_NML/ N, R, C10 N = 42; R = 1.234; C10 = "HELLO" OPEN (UNIT=12, FILE="FOO.TXT") WRITE (UNIT=12, NML=MY_NML) CLOSE(UNIT=12) END PROGRAM P $ cat FOO.TXT &MY_NML N= 42, R= 1.2340000 , C10="HELLO ", / $ cat FOO.TXT &MY_NML N= 42, R= 1.2340000 , C10="HELLO ", / Remember that this is formatted output You can use it similarly for READ REWIND(UNIT=12) ! Move to beginning N = 0 READ (UNIT=12, NML=MY_NML) IF (N /= 42) STOP "ERROR!" REWIND(UNIT=12) ! Move to beginning N = 0 READ (UNIT=12, NML=MY_NML) IF (N /= 42) STOP "ERROR!"
  24. Programming Models 27 Namelist formatting (2) • Namelist formatting, though

    handy, is not popular since it cannot be effectively modularized MODULE M IMPLICIT NONE INTEGER :: N REAL :: R CHARACTER(LEN=10) :: C10 NAMELIST /MY_NML/ N, R, C10 END MODULE M PROGRAM P USE M IMPLICIT NONE OPEN(UNIT=12, FILE="FOO.TXT") READ (UNIT=12, NML=MY_NML) !! MODIFIES M.N, M.R and M.C10 global vars! END PROGRAM P MODULE M IMPLICIT NONE INTEGER :: N REAL :: R CHARACTER(LEN=10) :: C10 NAMELIST /MY_NML/ N, R, C10 END MODULE M PROGRAM P USE M IMPLICIT NONE OPEN(UNIT=12, FILE="FOO.TXT") READ (UNIT=12, NML=MY_NML) !! MODIFIES M.N, M.R and M.C10 global vars! END PROGRAM P One cannot bind a same namelist to different variables. This forces us to repeat the namelist definition in every program unit.
  25. Programming Models 28 Input/output item-lists (1) • Data transfer statements

    may receive an input/output item list – Similar to an enhanced expression list • For WRITE and PRINT the value of the items is transferred as part of the output • For READ, the items must be variables and their values are set using the transferred input
  26. Programming Models 29 Input/output item list (2) • In formatted

    transfers, what is being transferred depends on the format (FMT=) – Later, we will see the format specification syntax PROGRAM P1 IMPLICIT NONE REAL :: R OPEN(UNIT=12, FILE="FOO.TXT") R = 1.2345 WRITE(UNIT=12, FMT="(F10.5)") R CLOSE(UNIT=12) END PROGRAM P2 PROGRAM P1 IMPLICIT NONE REAL :: R OPEN(UNIT=12, FILE="FOO.TXT") R = 1.2345 WRITE(UNIT=12, FMT="(F10.5)") R CLOSE(UNIT=12) END PROGRAM P2 PROGRAM P2 IMPLICIT NONE INTEGER :: N OPEN(UNIT=12, FILE="FOO.TXT") N = 0 READ(UNIT=12, FMT="(I6)") N ! Rubbish... PRINT *, N CLOSE(UNIT=12) END PROGRAM P2 PROGRAM P2 IMPLICIT NONE INTEGER :: N OPEN(UNIT=12, FILE="FOO.TXT") N = 0 READ(UNIT=12, FMT="(I6)") N ! Rubbish... PRINT *, N CLOSE(UNIT=12) END PROGRAM P2 Write a real number in a field of 10 characters where 5 are decimals Read a field of 6 characters and interpret it as an integer
  27. Programming Models 30 Reading records (1) • When reading formatted

    files we can choose to read – the whole record – a part of the record • Advancing data transfers always move to the next record even if not all the record has been read • We can read a part of the record doing nonadvancing data transfers – But we cannot go after the end of the current record!
  28. Programming Models 31 Reading records (2) PROGRAM P IMPLICIT NONE

    INTEGER :: A, B OPEN(UNIT=12, FILE="FOO.TXT") ! First record [1, 2] WRITE (UNIT=12, FMT="(I6,I6)") 1, 2 ! Second record [3, 4] WRITE (UNIT=12, FMT="(I6,I6)") 3, 4 CLOSE (UNIT=12) OPEN(UNIT=12, FILE="FOO.TXT", POSITION="REWIND") DO ! Infinite loop ! ADVANCE="YES" by default READ (UNIT=12, FMT="(I6)") A PRINT *, A END DO CLOSE(UNIT=12) END PROGRAM P PROGRAM P IMPLICIT NONE INTEGER :: A, B OPEN(UNIT=12, FILE="FOO.TXT") ! First record [1, 2] WRITE (UNIT=12, FMT="(I6,I6)") 1, 2 ! Second record [3, 4] WRITE (UNIT=12, FMT="(I6,I6)") 3, 4 CLOSE (UNIT=12) OPEN(UNIT=12, FILE="FOO.TXT", POSITION="REWIND") DO ! Infinite loop ! ADVANCE="YES" by default READ (UNIT=12, FMT="(I6)") A PRINT *, A END DO CLOSE(UNIT=12) END PROGRAM P $ ./test 1 3 At line 15 of file test.f90 (unit = 12, file = 'FOO.TXT') Fortran runtime error: End of file $ ./test 1 3 At line 15 of file test.f90 (unit = 12, file = 'FOO.TXT') Fortran runtime error: End of file 1 3 2 4 EOF R1 R2 R3 Records This (roughly) means transfer one integer
  29. Programming Models 32 Reading records (2) PROGRAM P IMPLICIT NONE

    INTEGER :: A, B OPEN(UNIT=12, FILE="FOO.TXT") ! First record [1, 2] WRITE (UNIT=12, FMT="(I6,I6)") 1, 2 ! Second record [3, 4] WRITE (UNIT=12, FMT="(I6,I6)") 3, 4 CLOSE (UNIT=12) OPEN(UNIT=12, FILE="FOO.TXT", POSITION="REWIND") DO ! Infinite loop READ (UNIT=12, FMT="(I6)", ADVANCE="NO") A PRINT *, A END DO CLOSE(UNIT=12) END PROGRAM P PROGRAM P IMPLICIT NONE INTEGER :: A, B OPEN(UNIT=12, FILE="FOO.TXT") ! First record [1, 2] WRITE (UNIT=12, FMT="(I6,I6)") 1, 2 ! Second record [3, 4] WRITE (UNIT=12, FMT="(I6,I6)") 3, 4 CLOSE (UNIT=12) OPEN(UNIT=12, FILE="FOO.TXT", POSITION="REWIND") DO ! Infinite loop READ (UNIT=12, FMT="(I6)", ADVANCE="NO") A PRINT *, A END DO CLOSE(UNIT=12) END PROGRAM P $ ./test 1 2 At line 15 of file test.f90 (unit = 12, file = 'FOO.TXT') Fortran runtime error: End of record $ ./test 1 2 At line 15 of file test.f90 (unit = 12, file = 'FOO.TXT') Fortran runtime error: End of record 1 3 2 4 EOF R1 R2 R3 Records
  30. Programming Models 33 Errors in IO (1) • When an

    error happens and there is an ERR=label specifier, the program jumps to the label • When an end of file happens and there is an EOF=label specifier, the program jumps to the label • When and end of record happens and there is an EOR=label specifier, the program jumps to the label • If there is either an error, or end of file or end of record and there is a IOSTAT=intvar specifier, intvar will be set a a nonzero value – Negative values for errors – Two different positive values for end-of-file and end-of-record You better use IOSTAT and forget about ERR, EOF and EOR specifiers
  31. Programming Models 34 Errors in IO (2) INTEGER :: io_status

    ... READ(UNIT=12, FMT="(I6)", IOSTAT=io_status) A IF (io_status /= 0) THEN PRINT *, "ERROR!" IF (io_status > 0) THEN PRINT *, "EOR or EOF" ELSE PRINT *, "Another error :-/" END IF END IF INTEGER :: io_status ... READ(UNIT=12, FMT="(I6)", IOSTAT=io_status) A IF (io_status /= 0) THEN PRINT *, "ERROR!" IF (io_status > 0) THEN PRINT *, "EOR or EOF" ELSE PRINT *, "Another error :-/" END IF END IF The values returned for EOF and EOR are not specified in Fortran 95 (check your compiler!) In Fortran 2003 one can use the intrinsic module ISO_FORTRAN_ENV which provides the constants IOSTAT_END and IOSTAT_EOR
  32. Programming Models 35 Positioning statements (1) • BACKSPACE – Only

    allows UNIT= and ERR= (jump on error) – If we are not inside a record, moves the file position to the preceding record – If we are inside a record, moves the file position to the beginning of the current record BACKSPACE (UNIT = 12) ! Moves backward one record BACKSPACE (UNIT = 12) ! Moves backward one record ! Alternate syntax BACKSPACE 12 ! Alternate syntax BACKSPACE 12
  33. Programming Models 36 Positioning statements (2) • REWIND – Positions

    the file to its initial position REWIND (UNIT = 12) ! Moves to the initial position REWIND (UNIT = 12) ! Moves to the initial position ! Alternate syntax REWIND 12 ! Alternate syntax REWIND 12
  34. Programming Models 37 Positioning statements (3) • ENDFILE – Writes

    an endfile record to the file – Nothing can be read/written after ENDFILE unless we reposition the file using BACKSPACE or REWIND ENDFILE (UNIT = 12) ! Moves to the initial position ENDFILE (UNIT = 12) ! Moves to the initial position ! Alternate syntax ENDFILE 12 ! Alternate syntax ENDFILE 12
  35. Programming Models 38 INQUIRE statement Fortran IO hodgepodge statement •

    It allows to get the current properties and status of a file unit, but there are a miriad of properties • Two basic forms INQUIRE(inquire-spec-list) INQUIRE(IOLENGTH = int-var) output-item-list • Needs one of UNIT= or FILE= – both is invalid
  36. Programming Models 39 Inquire specifiers [ UNIT = ] external-file-unit

    Inquire on a file unit FORMATTED = char-var Returns "YES" if FORMATTED access is valid FILE = file-name-expr Inquire on a file name UNFORMATTED = char-var Returns "YES" if UNFORMATTED access is valid IOSTAT = int-var Get result of last IO RECL = int-var Record length of file unit ERR = label Jumps to label on error NEXTREC = int-var Next record number EXIST = logical-var States if the file (name or unit) exists BLANK = char-var Get BLANK as in OPEN OPENED = logical-var States if this file name is open in any unit. States if the file unit is opened POSITION = char-var Return POSITION of OPEN NUMBER = int-var Returns the number of the program unit connected to the file name -1 if none is ACTION = char-var Returns ACTION of OPEN NAMED = logical-var States if the file unit is named READ = char-var Returns YES if readable NAME = char-var If the file has a name, its name WRITE = char-var Returns YES if writeable ACCESS = char-var Get access. Values as in OPEN READWRITE = char-var Returns YES if "readwriteable" SEQUENTIAL = char-var Returns "YES" if SEQUENTIAL is a valid access mode in this file DELIM = char-var DELIM as set in OPEN DIRECT = char-var Returns "YES" if DIRECT is a valid access PAD = char-var Padding character as in OPEN FORM = char-var Get formatting. Same values as in OPEN
  37. Programming Models 40 Recap of IO statements OPEN File connection

    BACKSPACE File positioning CLOSE File connection ENDFILE File positioning READ Input data transfer REWIND File positioning WRITE Output data transfer FLUSH Fortran 2003 PRINT Output data transfer WAIT Fortran 2003 INQUIRE File inquiry
  38. Programming Models 42 Format specification • FORMAT and FMT= is

    the Fortran way to specify the format of a formatted record ☺ • It is an entirely different language to Fortran so it has a different syntax of its own – Think it as the grand-grand-father of the first parameter of printf
  39. Programming Models 43 Format specifier (1) • A format specifier

    in a io-spec-list can be specified in two different ways in the FMT= specifier • Literal string with a format specifier inside • Label to a FORMAT statement elsewhere in the current program unit UNIT=12, FMT="(format-specifier)", ... UNIT=12, FMT='(format-specifier)', ... UNIT=12, FMT="(format-specifier)", ... UNIT=12, FMT='(format-specifier)', ... UNIT=12, FMT=1812 UNIT=12, FMT=1812 1812 FORMAT(format-specifier) 1812 FORMAT(format-specifier) Note, this is like what goes after the FORMAT keyword in a FORMAT statement but inside a string expression. Mind the parentheses!!!
  40. Programming Models 44 Format specifier (2) • A format specifier

    is a list of format items – comma separated but compilers are pretty liberal • Every format item is – a data edit descriptor preceded by an optional repeat (an integer literal) – a control edit descriptor – a character string edit descriptor – another format item list enclosed in parentheses and preceded by an optional repeat (an integer literal) format-item [rep] data-edit-desc → → control-edit-desc → char-string-edit-desc → [rep] ( format-item-list ) rep → integer literal
  41. Programming Models 45 Data edit descriptors Descriptor Meaning Descriptor Meaning

    Iw[.m] Integer editing. Read or write a decimal integer of w positions. On output, m digits are emitted, left- padding with zeroes if needed Ew.d[Ee] Real editing. Read or write a float of width w and d decimals including a decimal exponent like 1.2345e-1 ranging from 0 to 999. Larger exponents widths can be achieved with e > 3 Bw[.m] Like Iw[.m] but binary integers ENw.d[Ew] Like Ew.d[Ee] but using engineering notation where exponent is divisible by 3 Ow[.m] Like Iw[.m] but octal integers ESw.d[Ee] Like Ew.d[E] but using scientific notation where the significand ranges from 1 to 9 Zw[.m] Like Iw[.m] but hexadecimal integers A[w] Character editing. Read or write w characters Fw.d Real editing. Read or write a float of width w and d decimals like 0.2345 Lw Logical editing (.TRUE. or .FALSE.) Gw.d[Ee] Generalized editing. Like Iw for integers. Like Ew.d[Ee] for reals. Like Lw for logicals. Like Aw for character w, m, d and e are integer literals [ and ] mean optional parts When doing input w must be >0
  42. Programming Models 46 Examples (1) PROGRAM P PRINT "(I0)", 123

    PRINT "(I1)", 123 PRINT "(I2)", 123 PRINT "(I3)", 123 PRINT "(I4)", 123 PRINT "(I5)", 123 PRINT "(I10.6)", 123 END PROGRAM P PROGRAM P PRINT "(I0)", 123 PRINT "(I1)", 123 PRINT "(I2)", 123 PRINT "(I3)", 123 PRINT "(I4)", 123 PRINT "(I5)", 123 PRINT "(I10.6)", 123 END PROGRAM P $ ./prova 123 * ** 123 123 123 000123 $ ./prova 123 * ** 123 123 123 000123 This is the output emitted by gfortran. Other compilers may have subtle differences in the optional parts of the output. PROGRAM P PRINT "(2L1)", .TRUE., .FALSE. PRINT "(2L3)", .TRUE., .FALSE. PRINT "(2L7)", .TRUE., .FALSE. END PROGRAM P PROGRAM P PRINT "(2L1)", .TRUE., .FALSE. PRINT "(2L3)", .TRUE., .FALSE. PRINT "(2L7)", .TRUE., .FALSE. END PROGRAM P $ ./prova TF T F T F $ ./prova TF T F T F PROGRAM P PRINT "(2A)", "HELLO", "WORLD" PRINT "(2A6)", "HELLO", "WORLD" PRINT "(2A12)", "HELLO", "WORLD" END PROGRAM P PROGRAM P PRINT "(2A)", "HELLO", "WORLD" PRINT "(2A6)", "HELLO", "WORLD" PRINT "(2A12)", "HELLO", "WORLD" END PROGRAM P $ ./prova HELLOWORLD HELLO WORLD HELLO WORLD $ ./prova HELLOWORLD HELLO WORLD HELLO WORLD
  43. Programming Models 47 Examples (2) $ ./prova 123.560 123.5599976 123.559997558593750

    ***** ***** ***** 123.560 123.5599976 123.559997558593750 $ ./prova 123.560 123.5599976 123.559997558593750 ***** ***** ***** 123.560 123.5599976 123.559997558593750 PROGRAM P PRINT "(F0.3)", 123.56 PRINT "(F0.7)", 123.56 PRINT "(F0.15)", 123.56 PRINT "(F5.3)", 123.56 PRINT "(F5.7)", 123.56 PRINT "(F5.15)", 123.56 PRINT "(F20.3)", 123.56 PRINT "(F20.7)", 123.56 PRINT "(F20.15)", 123.56 END PROGRAM P PROGRAM P PRINT "(F0.3)", 123.56 PRINT "(F0.7)", 123.56 PRINT "(F0.15)", 123.56 PRINT "(F5.3)", 123.56 PRINT "(F5.7)", 123.56 PRINT "(F5.15)", 123.56 PRINT "(F20.3)", 123.56 PRINT "(F20.7)", 123.56 PRINT "(F20.15)", 123.56 END PROGRAM P PROGRAM P PRINT "(EN20.3)", 0.123456 PRINT "(EN20.7)", 0.123456 PRINT 1024, 0.123456 1024 FORMAT (EN40.15) END PROGRAM P PROGRAM P PRINT "(EN20.3)", 0.123456 PRINT "(EN20.7)", 0.123456 PRINT 1024, 0.123456 1024 FORMAT (EN40.15) END PROGRAM P $ ./prova 123.456E-03 123.4560013E-03 123.456001281738281E-03 $ ./prova 123.456E-03 123.4560013E-03 123.456001281738281E-03
  44. Programming Models 48 Control edit descriptors • There are several

    kind of control edit descriptors – Position ending T, TL, TR and X – Slash editing / – Colon editing : – S, SP and SS editing – P editing – BN and BZ editing
  45. Programming Models 49 Positioning ending • TRn nX – advance

    n characters forward • TLn – move backwards n characters or until the beginning of the record • Tn – advance n characters forward relative to the first character of the record
  46. Programming Models 50 Examples of position ending PROGRAM P INTEGER,

    PARAMETER :: X = 123 PRINT "(I0)", X PRINT "(1X,I0)", X PRINT "(TR1,I0)", X PRINT "(T3,I0)", X PRINT * PRINT "(I0,I0)", X, X PRINT "(T3,I0,T2,I0)", X, X PRINT "(I0,TL1,I0)", X, X END PROGRAM P PROGRAM P INTEGER, PARAMETER :: X = 123 PRINT "(I0)", X PRINT "(1X,I0)", X PRINT "(TR1,I0)", X PRINT "(T3,I0)", X PRINT * PRINT "(I0,I0)", X, X PRINT "(T3,I0,T2,I0)", X, X PRINT "(I0,TL1,I0)", X, X END PROGRAM P $ ./prova 123 123 123 123 123123 1233 12123 $ ./prova 123 123 123 123 123123 1233 12123
  47. Programming Models 51 Colon and slash • Colon (:) ends

    format if there are no more items • Slash (/) ends the current record $ ./prova 123 123 $ ./prova 123 123 PROGRAM P INTEGER, PARAMETER :: X = 123 PRINT "(I0/I0)", X, X END PROGRAM P PROGRAM P INTEGER, PARAMETER :: X = 123 PRINT "(I0/I0)", X, X END PROGRAM P PROGRAM P INTEGER :: A A = 3 PRINT "(I3:' HELLO')", A PRINT "(I3' HELLO')", A END PROGRAM P PROGRAM P INTEGER :: A A = 3 PRINT "(I3:' HELLO')", A PRINT "(I3' HELLO')", A END PROGRAM P $ ./format 3 3 HELLO $ ./format 3 3 HELLO
  48. Programming Models 52 S, SS and SP • SP means

    there must be a + where it would be otherwise optional • SS means there is no + where it would be otherwise optional • S restores + its optional status PROGRAM P INTEGER, PARAMETER :: X = 123 PRINT "(SS,I0,I0)", X, X PRINT "(SP,I0,I0)", X, X PRINT "(S,I0,I0)", X, X END PROGRAM P PROGRAM P INTEGER, PARAMETER :: X = 123 PRINT "(SS,I0,I0)", X, X PRINT "(SP,I0,I0)", X, X PRINT "(S,I0,I0)", X, X END PROGRAM P $ ./prova 123123 +123+123 123123 $ ./prova 123123 +123+123 123123
  49. Programming Models 53 P editing • kP editing modifies the

    scale factor of numbers in data edit descriptors F, EN, ES, D and G by powers of 10 $ ./prova 123.2400 12323.9998 123239.9979 1232399.9786 $ ./prova 123.2400 12323.9998 123239.9979 1232399.9786 PROGRAM P REAL, PARAMETER :: X = 123.24 PRINT "(F12.4)", X PRINT "(2P,F12.4)", X PRINT "(3P,F12.4)", X PRINT "(4P,F12.4)", X END PROGRAM P PROGRAM P REAL, PARAMETER :: X = 123.24 PRINT "(F12.4)", X PRINT "(2P,F12.4)", X PRINT "(3P,F12.4)", X PRINT "(4P,F12.4)", X END PROGRAM P
  50. Programming Models 54 BN and BZ Only during input •

    BN ignores all nonleading blanks before an integer field • BZ all nonleading blanks are considered as zeros PROGRAM P CHARACTER(LEN=50) :: C INTEGER :: N C = " 1 2 3 4" READ (UNIT=C, FMT="(I6)") N PRINT *, N READ (UNIT=C, FMT="(BN,I6)") N PRINT *, N READ (UNIT=C, FMT="(BZ,I6)") N PRINT *, N END PROGRAM P PROGRAM P CHARACTER(LEN=50) :: C INTEGER :: N C = " 1 2 3 4" READ (UNIT=C, FMT="(I6)") N PRINT *, N READ (UNIT=C, FMT="(BN,I6)") N PRINT *, N READ (UNIT=C, FMT="(BZ,I6)") N PRINT *, N END PROGRAM P $ ./prova 123 123 10203 $ ./prova 123 123 10203
  51. Programming Models 55 Character edit descriptors • A literal string

    that can be used only for output PROGRAM P PRINT "('HELLO WORLD 'I0)", 42 PRINT '("HELLO WORLD "I0)', 42 END PROGRAM P PROGRAM P PRINT "('HELLO WORLD 'I0)", 42 PRINT '("HELLO WORLD "I0)', 42 END PROGRAM P $ ./prova HELLO WORLD 42 HELLO WORLD 42 $ ./prova HELLO WORLD 42 HELLO WORLD 42
  52. Programming Models 56 List directed input/output • When FMT=* the

    data transfer is a list-directed input/output • In list-directed formatting the input is considered to be a sequence of values and value-separators • Values are – constant – repeat*constant – repeat* • Separators are – comma (surrounded by any amount of blanks) – slash (surrounded by any amount of blanks) – blanks
  53. Programming Models 57 Example PROGRAM P CHARACTER(LEN=100) :: C INTEGER

    :: A(2) REAL :: B TYPE T INTEGER :: X, Y, Z END TYPE T TYPE(T) :: D C = "2*10, 1.23, 24, 2, 3" READ (UNIT=C, FMT=*) A, B, D PRINT *, A, B, D END PROGRAM P PROGRAM P CHARACTER(LEN=100) :: C INTEGER :: A(2) REAL :: B TYPE T INTEGER :: X, Y, Z END TYPE T TYPE(T) :: D C = "2*10, 1.23, 24, 2, 3" READ (UNIT=C, FMT=*) A, B, D PRINT *, A, B, D END PROGRAM P The input is matched with every variable in a flat way A 2 integers → B 1 real → D 3 integers (X, Y, Z) → Input contains a 2-repeat of the integer literal 10, a floating literal 1.23 and integer literals 24, 2, and 3 $ ./prova 10 10 1.2300000 24 2 3 $ ./prova 10 10 1.2300000 24 2 3