Partly due to just how many kinds of architectures Fortran has been around figuring out the number of records in a direct access file can be platform-specific. It can depend on which compiler and compiler options you choose and what hardware you are operating on. But usually now-adays query the size of the file and divide by the record length in bytes and you probably have the number of records.
Just be aware that not all OPEN statements are using bytes as the unit for the RECL option and that some rare environments that store files in interesting ways, usually for optimization purposes can cause other problems.
So if you are using one compiler with compiler options you are specifying this is easy; else it gets into a much longer discussion. I put a few hints on what else to look up if you do not have the simple case:
program show
use ISO_FORTRAN_ENV, only : FILE_STORAGE_SIZE
implicit none
integer :: nexrec, ios, lun, i, sz, likely
character(len=256) :: message
integer,parameter :: ll=160
character(len=ll) :: line
! first program makes a file
open(newunit=lun, file='DBPP', access='direct', &
& status= 'replace', recl=ll,form='unformatted', &
& iostat=ios, iomsg=message)
do i=26,1,-1
line=repeat(char(ichar('a')+i-1),ll)
write(lun,rec=i)line
enddo
close(lun)
write(*,*)'made and closed sample file'
INQUIRE(IOLENGTH=likely) line
! second program reads a file
open(newunit=lun, file='DBPP', access='direct', &
& status= 'old', recl=ll,form='unformatted', &
& iostat=ios, iomsg=message)
if(ios.eq.0)then
inquire(unit=lun, nextrec=nexrec,size=sz)
print *,'the next record is ', nexrec
print *,'the size is ', sz
print *,'the file storage unit size is ', file_storage_size
print *,'the iolength of a line is', likely
if(likely.ne.ll)then ! not in bytes
print *,'and your file is probably bigger than you need'
print *,'because your RECL units are not bytes'
print *,'the number of lines is probably ',sz/ll/(ll/likely)
else
print *,'your file probably has', sz/ll, 'records'
endif
else
print *, '<ERROR>'//trim(message)
stop 1
endif
print *, 'ready'
end program show
One common case is when using ifort(1) it depends on whether you compile with -assume byterecl or not, and so on, but probably all you need is:
program show
implicit none
integer :: ios, lun, sz
character(len=256) :: message
integer,parameter :: ll=160
open(newunit=lun, file='DBPP', access='direct', &
& status= 'old', recl=ll,form='unformatted', &
& iostat=ios, iomsg=message)
if(ios.eq.0)then
inquire(unit=lun, size=sz)
print *,'the size is ', sz
print *,'your file probably has', sz/ll, 'records'
else
print *, '<ERROR>'//trim(message)
stop 1
endif
print *, 'ready'
end program show