So both the gfortran and ifort versions I am running appear to have the file in the same state until program termination, where they do not truncate; but overwrite. But at program termination ifort truncates the file at the current position; and gfortran adds an end-of-line at the current position,
even if the current position was set with the non-advancing I/O statement with no parameters; which the standard indicates per the above reference should do nothing but change the position in the file.
So I would say they are both wrong at program termination, and that the files should NOT be truncated
except perhaps if a call to ENDFILE was made(?).
program t
implicit none
integer,parameter :: isz=10
integer :: i, iu, ipos(isz)=1, isize
open(newunit=iu, file='overwrite.txt', access='stream', form='formatted')
do i=1,isz
inquire(iu,pos=ipos(i))
write(iu,*)'write=',i,'ipos=',ipos(i)
enddo
inquire(iu,size=isize)
flush(iu)
call execute_command_line('cat overwrite.txt')
write(*,*)'file size is',isize
write(iu,*,pos=ipos(8))'new eigth line'
inquire(iu,size=isize)
flush(iu)
call execute_command_line('cat overwrite.txt')
write(*,*)'file size is',isize
write(iu,'(a)',pos=ipos(3),advance='no')'new third line'
inquire(iu,size=isize)
flush(iu)
call execute_command_line('cat overwrite.txt')
write(*,*)'file size is',isize
write(iu,'(a)',pos=ipos(6),advance='no')
inquire(iu,size=isize)
flush(iu)
call execute_command_line('cat overwrite.txt')
write(*,*)'file size is',isize
end program t
$ gfortran --version
GNU Fortran (Ubuntu 13.1.0-8ubuntu1~20.04.2) 13.1.0
$ ifort --version
ifort (IFORT) 2021.10.0 20230609
So after the program terminates the output file should contain 10, 8, 6, or 3 lines? and what should the file contents be? Both compilers truncate to the 8th line on the advancing I/O and overwrite on the 3rd,
but then change the file on program termination differently. The standard implies I should be able to return to any position returned by INQUIRE, which creates an interesting catch-22 with regards to the truncation. And then if I return to the position captured in IPOS(10) where am I? I have changed the file since I recorded the position. I found that even though both compilers appear to have truncated the file to line eight because of the advancing I/O that I can still return to POS=IPOS(10) without an error but if I then write it is appended to line eight and does not restore the filesize to what it was when I recorded IPOS(10); so I find the positions confusing; particularly if I have written other positions. Is anyone else as confused as I am as to what the output of the file should be after running the following, after each stop?
test program
program t
implicit none
integer,parameter :: isz=10
integer :: i, iu, ipos(isz)=1, isize, qpos
open(newunit=iu, file='overwrite.txt', access='stream', form='formatted')
do i=1,isz
inquire(iu,pos=ipos(i))
write(iu,*)'write=',i,'ipos=',ipos(i)
enddo
inquire(iu,size=isize,pos=qpos)
flush(iu)
call execute_command_line('cat overwrite.txt')
write(*,*)'file size is',isize,qpos
write(iu,*,pos=ipos(8))'new eigth line'
inquire(iu,size=isize,pos=qpos)
flush(iu)
call execute_command_line('cat overwrite.txt')
write(*,*)'file size is',isize,qpos
write(iu,'(a)',pos=ipos(3),advance='no')'new third line'
inquire(iu,size=isize,pos=qpos)
flush(iu)
call execute_command_line('cat overwrite.txt')
write(*,*)'file size is',isize,qpos
! stop 1
! position with no I/O
write(iu,'(a)',pos=ipos(6),advance='no')
inquire(iu,size=isize,pos=qpos)
flush(iu)
call execute_command_line('cat overwrite.txt')
write(*,*)'file size is',isize,qpos
! stop 2
write(iu,'(a)',pos=ipos(10),advance='no')
inquire(iu,size=isize,pos=qpos)
flush(iu)
call execute_command_line('cat overwrite.txt')
write(*,*)'file size is',isize,qpos
! stop 3
write(*,*)'BACK TO ',ipos(10)
write(iu,'(a)',pos=ipos(10),advance='no')'EOF'
inquire(iu,size=isize,pos=qpos)
flush(iu)
call execute_command_line('cat overwrite.txt')
write(*,*)'file size is',isize,qpos
!stop 4
end program t
The more I played with the program and those two compilers the more uncertain I became as to what I should expect in the file. Is this worth asking for clarification from J3 or does someone know for certain
what should be truncated and where output should go for this little test case?