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

Van Snyder Van.Snyder
Mon Feb 27 15:50:27 EST 2012

On Mon, 2012-02-27 at 01:19 -0800, Tobias Burnus wrote:
> 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:
> !----------------------------------------------
>         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
> !----------------------------------------------

I have a vague recollection that at least some processors prohibited
read-after-write and write-after-read without an intervening REWIND
statement.  As far as I can determine, there's no such prohibition in
any standard.

But... did we really intend that write-after-nonadvancing-read would be
permitted?  If so, we really ought to define it more precisely.

> (Variant: Replace the last ADVANCE='NO' by 'YES'.)
> Result:
> (a) Pathscale 4.0, Openf95 5.0, OpenUH/Open64
> (b) gfortran (4.1 to 4.7), NAG 5.1, Lahey:
> (c) g95, Silverfrost ftn95, ifort 11.1/12.1
> [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)*
> (* This compiler needs some message to get this result)
> Some relevant parts of the Fortran 2008 standard:
> Paragraph 2 of Clause 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:
> " 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. [...]"
> " 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 -----
> _______________________________________________
> J3 mailing list
> J3 at j3-fortran.org
> http://j3-fortran.org/mailman/listinfo/j3

More information about the J3 mailing list