Limit on the name of data file (character variable)

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.

I am not quite sure what the problem is that you are encountering. Each system has a limit as to the length of file names, but the name you use in the sample is not excessively long. Could it be that together with the directory in which it is stored, so the path name, the result is too long?
What system are you working on?

I am using Linux OS with Ubuntu 20.04.6 LTS. The program script and data file are in the same directory. Now: I copied all the stuff to a new directory and it worked. Not sure what is the problem?

update: probably issue is the file permission as the file was copied from a USB stick with different permission.