(j3.2006) Partial write in record with nonadvancing I/O

Tobias Burnus burnus
Mon Feb 27 04:19:09 EST 2012


Dear all,

at comp.lang.fortran, Bob Corbett queried what the different compilers output
for the following program - and the replies showed that the results vary a lot. Cf.
http://groups.google.com/group/comp.lang.fortran/browse_thread/thread/694050b1806da367

(I assume that he intended to post it here or write an IR.)

Which output(s) do you believe are standard conforming?

The program is the following and I think looking at the created file is even more
informative than looking at the OUTPUT_UNIT output of the program:

!----------------------------------------------
      PROGRAM MAIN
        CHARACTER*12 STR
        OPEN (10, FILE='XXX', POSITION='REWIND')
        WRITE (10, '(A)') 'ABCDEFGHIJKL'
        REWIND 10
        READ (10, '(TR4)', ADVANCE='NO')
        WRITE (10, '(TL2, A)', ADVANCE='NO') 'MNOP'
        REWIND 10
        READ (10, '(A)') STR
        PRINT '(A)', STR
      END
!----------------------------------------------

(Variant: Replace the last ADVANCE='NO' by 'YES'.)


Result:

(a) Pathscale 4.0, Openf95 5.0, OpenUH/Open64
ABCDMNOPIJKL

(b) gfortran (4.1 to 4.7), NAG 5.1, Lahey:
ABCDMNOP

(c) g95, Silverfrost ftn95, ifort 11.1/12.1
ABCDEFGHIJKL
[Where "MNOP" is in the next record, either at position 1 or
 at position 3.]

(d) sunf95/Oracle f95, IBM xlf, Microsoft V 3.31 (1985)*
MNOP

(* This compiler needs some message to get this result)


Some relevant parts of the Fortran 2008 standard:

Paragraph 2 of Clause 9.3.4.2 has the following:

"A nonadvancing input/output statement may position a record file at a
character position within the current record, or a subsequent record
(10.8.2). Using nonadvancing input/output, it is possible to read or
write a record of the file by a sequence of input/output statements,
each accessing a portion of the record. It is also possible to read
variable-length records and be notified of their lengths. If a
nonadvancing output statement leaves a file positioned within a current
record and no further output statement is executed for the file before
it is closed or a BACKSPACE, ENDFILE, or REWIND statement is executed
for it, the effect is as if the output statement were the corresponding
advancing output statement."


C.6.2, paragraph 4 (nonnormative):

"If the next I/O operation on a file after a nonadvancing write is a
rewind, backspace, end file or close operation, the file is positioned
implicitly after the current record before an ENDFILE record is written
to the file, that is, a REWIND, BACKSPACE, or ENDFILE statement
following a nonadvancing WRITE statement causes the file to be
positioned at the end of the current output record before the endfile
record is written to the file."

Interpretation request Fortran 95/000024:
http://j3-fortran.org/doc/year/02/02-006c2.txt

Other Fortran 2008 quotes:

"9.3.4.3 File position prior to data transfer" (para. 2 and 4):
"For sequential access on input, if there is a current record, the file
position is not changed. [...]"
"For sequential access on output, if there is a current record, the file
position is not changed and the current record becomes the last record
of the file. [...]"

"9.3.4.4 File position after data transfer" (para. 4 and 5):
"For nonadvancing input, if no error condition or end-of-file condition
occurred, but an end-of-record condition (9.11) occurred, the file is
positioned after the record just read. If no error condition,
end-of-file condition, or end-of-record condition occurred in a nonadvancing
input statement, the file position is not changed. If no error condition
occurred in a nonadvancing output statement, the file position is not changed.
"In all other cases, the file is positioned after the record just read or
written and that record becomes the preceding record."


Tobias

PS: Bob's expected output is (a): "ABCDMNOPIJKL", I'm more inclined to (b),
but Bob could have the better arguments.

----- End forwarded message -----



More information about the J3 mailing list