Richard Maine wrote:
> That seems like an excellent point. The nonadvancing stuff just
> complicates the point. Go back to the simplest case, as Stan shows.
> Though I couldn't find it quite explicit in the standard, I'm *SURE*
> that there would be lots of majorly pissed users if the program
>
> program bad
> open(10,file='xxx')
> write (10,'(a)') "some text"
> end
>
> ended up with a file that had additional stuff after the "some text"
> just because there was a file with other content before this program
> ran.
Well, that example is easier as there is no nonadvancing read involved -
and as the file is closed with termination of the program.
Besides the issue of nonadvancing read, I think it is also interesting
to see the result of the file after a FLUSH but before actually closing
it. With keeping a file open while accessing it from a different
program, I presumably leave the solid ground, but users do write such
codes. (At least on Unix, it is common that multiple programs access the
same file.)
The following program can be run in 8 different ways:
* With a (nonadvancing) READ - and without READ
* With advancing or nonadvancing WRITE
* With sequential or with stream I/O
Especially the result after the FLUSH but before closing (or REWIND or
ENDFILE) widely varies between different compilers.
Here is the program as I posted to the J3 mailing list
[The access='stream' was later suggested by Rafik, who also provided the
IBM xlf and gfortran/cygwin results.]
!--------------------------------------------------------
OPEN (10, FILE='XXX', status='replace')
WRITE(10, '(a)') 'ABCDEFGHIJKL'
WRITE(10, '(a)') '1234567890ab'
close (10)
OPEN (10, FILE='XXX', position='REWIND', status='old', &
action='readwrite', access='sequential', form='formatted')
! READ(10, '(tl1)', advance='no') ! << add optionally
WRITE (10, '(A)', ADVANCE='NO') 'mnop' ! << optionally: advance='yes'
FLUSH (10)
call execute_command_line ('cat XXX; echo "<"')
close(10)
call execute_command_line ('cat XXX; echo "<"')
end
!--------------------------------------------------------
(Replace "flush(10)" by "call flush(10)" and "execute_command_line"
by "system" if your compiler supports those instead of the new
F2003 statement/F2008 intrinsic. On Windows, the "cat" could be
replaced by "type". I had to do some of those for the results below.)
Results for several compilers:
a) Nonadvancing READ, nonadvancing WRITE
----------------------------------------
(i) Pathscale 3.2.99, Cray 7.1, Open64 4.2
mnopEFGHIJKL
1234567890ab
<
mnopEFGHIJKL
<
(ii) gfortran 4.7, PGI 11.5,
mnop<
mnop
<
(iii) g95 0.93, sunf95 8.3
ABCDEFGHIJKL
mnop<
ABCDEFGHIJKL
mnop<
(iv) ifort 12.1 [also with "-assume nobuffered_io"]
ABCDEFGHIJKL
1234567890ab
<
ABCDEFGHIJKL
mnop
<
(v) IBM XL Fortran
mnop<
mnop<
(iv) NAG f95 5.1
ABCDEFGHIJKL
mnop567890ab
<
ABCDEFGHIJKL
mnop
<
b) Nonadvancing READ, advancing WRITE
-------------------------------------
(i) Pathscale 3.2.99, Cray 7.1, Open64 4.2
mnopEFGHIJKL
1234567890ab
<
mnopEFGHIJKL
<
(ii) gfortran 4.7, PGI 11.5, IBM XL Fortran
mnop
<
mnop
<
(iii) g95 0.93, ifort 12.1, sunf95 8.3
ABCDEFGHIJKL
mnop
<
ABCDEFGHIJKL
mnop
<
(iv) NAG f95 5.1
ABCDEFGHIJKL
mnop
67890ab
<
ABCDEFGHIJKL
mnop
<
c) No READ, nonadvancing WRITE
------------------------------
(i) Pathscale 3.2.99, Cray 7.1, Open64 4.2, NAG f95 5.1
mnopEFGHIJKL
1234567890ab
<
mnop
<
(ii) gfortran 4.7, PGI 11.5,
mnop<
mnop
<
(iii) g95 0.93, IBM XL Fortran, sunf95 8.3
mnop<
mnop<
(iv) ifort 12.1 [-assume nobuffered_io]
ABCDEFGHIJKL
1234567890ab
<
mnop
<
d) No READ, advancing WRITE
------------------------------
(i) Pathscale 3.2.99, Cray 7.1, Open64 4.2, NAG f95 5.1
mnop
FGHIJKL
1234567890ab
<
mnop
<
(ii) gfortran 4.7, PGI 11.5, g95, ifort 12.1, IBM XL Fortran, sunf95 8.3
mnop
<
mnop
<
------------------------------------------------
With stream access instead of sequential access:
------------------------------------------------
a) Nonadvancing READ, nonadvancing WRITE
----------------------------------------
(i) IBM XL Fortran, g95 0.93, sunf95 8.3
mnopEFGHIJKL
1234567890ab
<
mnopEFGHIJKL
1234567890ab
<
(ii) gfortran 4.5.3 (cygwin)/4.7(Linux)
mnopEFGHIJKL
1234567890ab
<
mnop
FGHIJKL
1234567890ab
<
(iii) NAG f95 5.1, ifort 12.1
ABCDEFGHIJKL
mnop567890ab
<
ABCDEFGHIJKL
mnop
<
b) Nonadvancing READ, advancing WRITE
-------------------------------------
(i) IBM XL Fortran, g95 0.93
mnop
FGHIJKL
1234567890ab
<
mnop
FGHIJKL
1234567890ab
<
(ii) gfortran 4.5.3 (cygwin)
mnop
<
mnop
<
(iii) gfortran 4.7(Linux)
ABCDEFGHIJKL
1234567890ab
<
ABCDEFGHIJKL
mnop
67890ab
<
(iv) sunf95 8.3
ABCDEFGHIJKL
mnop567890ab
<
ABCDEFGHIJKL
mnop567890ab
<
(v) NAG f95 5.1
ABCDEFGHIJKL
mnop
67890ab
<
ABCDEFGHIJKL
mnop
<
(vi) ifort 12.1
ABCDEFGHIJKL
mnop
<
ABCDEFGHIJKL
mnop
<
c) No READ, nonadvancing WRITE
------------------------------
(i) IBM XL Fortran, g95 0.93, sunf95 8.3
mnopEFGHIJKL
1234567890ab
<
mnopEFGHIJKL
1234567890ab
<
(ii) gfortran 4.5.3 (cygwin)/4.7(Linux)
mnopEFGHIJKL
1234567890ab
<
mnop
FGHIJKL
1234567890ab
<
(iii) NAG f95 5.1
mnopEFGHIJKL
1234567890ab
<
mnop
<
(iv) ifort 12.1
ABCDEFGHIJKL
1234567890ab
<
mnop
<
d) No READ, advancing WRITE
------------------------------
(i) IBM XL Fortran, sunf95 8.3
mnop
FGHIJKL
1234567890ab
<
mnop
FGHIJKL
1234567890ab
<
(ii) gfortran 4.5.3 (cygwin)/4.7(Linux), g95 0.93, ifort 12.1
mnop
<
mnop
<
(iii) NAG f95 5.1
mnop
FGHIJKL
1234567890ab
<
mnop
<
Tobias