I'm reading a file into a character string like this:
character*50, intent(in) :: flname
character(len=:), allocatable :: str
integer size
inquire(file=flname, size=size)
allocate(character(len=size)::str)
open(1, file=flname, status='OLD', access='STREAM')
read(1) str
close(1)
I'm trying to loop over the character string and detect certain characters, including ones like the new line ('\n') or tab ('\t') characters. But for some reason, I cannot detect those characters in a file. Is Fortran automatically ignoring these characters and if so, how can I get it to detect them?
You should be able to directly compare with previously-specified characters, e.g. NL, CR, HT in the code below.
program test
integer i
character, parameter :: NL = new_line( 'A' ) ! new line
character, parameter :: CR = achar( 13 ) ! carriage return
character, parameter :: HT = achar( 9 ) ! horizontal tab
character(len=50) :: flname = "test.f90"
character(len=:), allocatable :: str
integer size
inquire(file=flname, size=size)
allocate(character(len=size)::str)
open( 1, file=flname, status='OLD', access='STREAM' )
read(1) str
close(1)
do i = 1, size
select case( str(i:i) )
case( NL ); write(*,*) "New line"
case( CR ); write(*,*) "Carriage return"
case( HT ); write(*,*) "Horizontal tab"
case default; write(*,*) "Letter [", str(i:i), "]"
end select
end do
end program test