On Fri, 7 Apr 2000, Orville E. Wheeler wrote:
> How does one determine, ab initio, how many records are
> written to an existing Direct Access File?
I append some code based on a routine I wrote some time ago which does
what I think you want. Note: may not work on some operating systems,
depending on how they treat reading a record which does not exist, and
will need modification if your file is _formatted_ direct-access.
Regards
--
Clive Page,
Dept of Physics & Astronomy,
University of Leicester.
=====================================================================
! finds number of records on existing direct-access unformatted file
subroutine lenfile(fname, lenrec, length)
implicit none
character*(*), intent(in) :: fname ! name of existing direct-access file
integer, intent(in) :: lenrec ! record length (O/S dependent units)
integer, intent(out) :: length ! number of records.
!
character :: cdummy*1
integer :: lunit, nlo, nhi, mid, kode
logical :: exists, open
!
! find a free unit on which to open the file
!
do lunit = 99, 1, -1
inquire(unit=lunit, exist=exists, opened=open)
if(exists .and. .not. open) exit
end do
open(unit=lunit, file=fname, access="direct", recl=lenrec, iostat=kode)
if(kode /= 0) then
print *, 'error in lenfile: ', trim(fname), ' does not exist'
return
end if
!
! expansion phase
!
mid = 1
do
read(lunit, rec=mid, iostat=kode) cdummy
if(kode /= 0) exit
mid = 2 * mid
end do
!
! length is between mid/2 and mid, do binary search to refine
!
nlo = mid/2
nhi = mid
do while(nhi - nlo > 1)
mid = (nlo + nhi) / 2
read(lunit, rec=mid, iostat=kode) cdummy
if(kode == 0) then
nlo = mid
else
nhi = mid
end if
end do
length = nlo
close(unit=lunit)
return
end subroutine lenfile
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|