Community
cancel
Showing results for 
Search instead for 
Did you mean: 
Highlighted
Beginner
18 Views

Writing to a stream formatted file and file truncation

Writing to a stream formatted file and file truncation

I tested the code originally posted by IanH at:

http://stackoverflow.com/questions/38176611/overwrite-a-file-using-fortran/38178770#38178770

and I get different results between Intel Fortran (ifort) and gfortran, as illustrated by the following bash session under Cygwin:

bash 1 : cat a.f90

PROGRAM stream

IMPLICIT NONE

INTEGER :: unit

REAL    :: r

INTEGER :: line, pos

OPEN( NEWUNIT=unit, FILE='stream.txt', STATUS='REPLACE', ACCESS='STREAM', &

      POSITION='REWIND', FORM='FORMATTED' )

CALL RANDOM_SEED()

! Remember where we are.  In this case, the position is the first file storage

! unit in the file, but it doesn't have to be.

INQUIRE(unit, POS=pos)

! Leave some space in the file for later overwriting with the number of lines.

! We'll stick the number zero in there for now.

WRITE (unit, "(I10)") 0

! Write out the varying number of lines.

line = 0

DO

   CALL RANDOM_NUMBER(r)

   IF (r < 0.05) EXIT

   line = line + 1

   PRINT "('Writing line ',I0)", line

   WRITE (unit, "('line ',I10)") line

END DO

! Now update the space at the start with the number of following "lines".

WRITE (unit, "(I10)", POS=pos) line

CLOSE(unit)

END PROGRAM stream

 

bash 2 : ifort  /what  2>&1  |  head -1

Intel(R) Visual Fortran Compiler for applications running on IA-32, Version 16.0.3.207 Build 20160415

bash 3 : ifort  /nologo  a.f90

bash 4 : ./a.exe

Writing line 1

Writing line 2

Writing line 3

Writing line 4

Writing line 5

Writing line 6

Writing line 7

bash 5 : cat  stream.txt

         7

line          1

line          2

line          3

line          4

line          5

line          6

line          7

bash 6 : gfortran  --version  |  head -1

GNU Fortran (GCC) 5.4.0

bash 7 : gfortran  a.f90

bash 8 : ./a.exe

Writing line 1

Writing line 2

Writing line 3

Writing line 4

Writing line 5

Writing line 6

Writing line 7

bash 9 : cat  stream.txt

         7

bash 10 :

 

In essence, the file stream.txt is opened as a STREAM FORMATTED file and writing is performed in a non-ending position. The file is truncated at the position following that WRITE by gfortran but not by ifort.

 

Under the heading "Formatted Stream Files", Clive Page states in the document:

http://www.star.le.ac.uk/~cgp/streamIO.html

"Another difference from unformatted stream output is that (according to my reading of the Standard) whenever a WRITE statement writes to a position preceding the end of the file, it has the effect of truncating the file at that position, i.e. all subsequent data in the file are lost."

 

Similarly, the F2008 standard draft states under 9.3.4.4:

"For a formatted stream output statement, if no error condition occurred, the terminal point of the file is set to the next position after the highest-numbered position to which data was transferred by the statement."

 

In these circumstances, can the result obtained with ifort be considered as a bug ?

 

Note that the non-truncating behavior seems correct if the file had been opened in STREAM UNFORMATTED mode.

 

0 Kudos
2 Replies
Highlighted
18 Views

This is a bug reported

This is a bug reported earlier and fixed, but the fix is not scheduled for a near-term release. Issue ID is DPD200411748. I will ask that it be added to a 17.0 update.

Retired 12/31/2016
0 Kudos
18 Views

This was fixed in 17.0 Update

This was fixed in 17.0 Update 1.

Retired 12/31/2016
0 Kudos