Skip to content

Commit 359b56c

Browse files
authored
fix(fseek_stream): position with read/write based on file action (MODFLOW-ORG#2846)
The fseek_stream subroutine assumed the file is open in write mode. If the file is open in read-only mode, use read instead to position the file pointer. Just a preemptive, defensive fix as this is a general-purpose module/subroutine.
1 parent 9c6f1f0 commit 359b56c

1 file changed

Lines changed: 10 additions & 2 deletions

File tree

src/Utilities/InputOutput.f90

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1717,6 +1717,7 @@ subroutine fseek_stream(iu, offset, whence, status)
17171717
integer(I4B), intent(inout) :: status
17181718
! -- local
17191719
integer(I8B) :: ipos
1720+
character(len=20) :: file_action
17201721
!
17211722
inquire (unit=iu, size=ipos)
17221723
!
@@ -1737,8 +1738,15 @@ subroutine fseek_stream(iu, offset, whence, status)
17371738
ipos = ipos + offset
17381739
end select
17391740
!
1740-
! -- position the file pointer to ipos
1741-
write (iu, pos=ipos, iostat=status)
1741+
! -- position the file pointer to ipos using read or write depending
1742+
! on the file action, since write fails on read-only files and
1743+
! read fails on write-only files
1744+
inquire (unit=iu, action=file_action)
1745+
if (trim(file_action) == 'READ') then
1746+
read (iu, pos=ipos, iostat=status)
1747+
else
1748+
write (iu, pos=ipos, iostat=status)
1749+
end if
17421750
inquire (unit=iu, pos=ipos)
17431751
end subroutine fseek_stream
17441752

0 commit comments

Comments
 (0)