module m_count_lines_mod
contains
function count_lines(filename) result(nlines)
implicit none
character(len=*) :: filename
integer :: nlines
integer :: io
open (10, file=filename, iostat=io, status='old')
if (io /= 0) stop 'Cannot open file! '
nlines = 0
do
read (10, *, iostat=io)
if (io /= 0) exit
nlines = nlines + 1
end do
close (10)
print*, nlines
end function count_lines
end module m_count_lines_mod
program test
use m_count_lines_mod, only: f_count_lines => count_lines
integer:: nline
character(len=40):: filename
nline = 0
!filename="abcdegIstheNameofFileErrorCAR"
filename="abcdegIstheNameofFileErrorCARXXXXXX"
print*, ' !filename contains 1 line with scalar variable'
print*, ' ! cat abcdegIstheNameofFileErrorCARXXXXXX'
print*, '23223'
nline = f_count_lines(filename)
print*, 'f_count_lines: lines in the file ', filename, ' are', nline , '!!'
end program
!output:
! f_count_lines: lines in the file abcdegIstheNameofFileErrorCARXXXXXX are 0 !!
Taking a small file name works well, not for big name. I am doing something wrong? In fact, the length is system-dependent Fortan wiki character and very very large.