Fortran Wiki
readline

An example of using f2003+ to read a line of arbitrary length …

NAME

readline(3f):
read a line of any length (up to programming environment limit) from stdin into allocatable string

SYNTAX

  subroutine readline(line,ier)
  character(len=:),allocatable,intent(out) :: line
  integer,intent(out) :: ier

DESCRIPTION

Read a line of any length up to programming environment’s maximum line length from stdin. Written in Fortran 2003+.

  • Append lines that end in a backslash with next line
  • Trim trailing white space

RESULTS

  • LINE - output line read from stdin
  • IER - error code. Zero (0) on successful read

program demo_readline
   implicit none
   character(len=:),allocatable :: line
   integer :: ier
   INFINITE: do
      call readline(line,ier)
      if(ier.ne.0)exit INFINITE
      write(*,'(a)')'['//line//']'
   enddo INFINITE
contains

subroutine readline(line,ier)
implicit none
character(len=:),allocatable,intent(out) :: line
integer,intent(out)                      :: ier

   integer,parameter                     :: buflen=1024
   character(len=buflen)                 :: buffer
   integer                               :: last
   integer                               :: isize

   line=''
   ier=0

   ! read characters from line and append to result
   INFINITE: do
      ! read next buffer (an improvement might be to use stream I/O
      ! for files other than stdin so system line limit is not
      ! limiting)
      read(*,iostat=ier,fmt='(a)',advance='no',size=isize) buffer
      ! append what was read to result
      if(isize.gt.0)line=line//buffer(:isize)
      ! if hit EOR reading is complete unless backslash ends the line
      if(is_iostat_eor(ier))then
         last=len(line)
         if(last.ne.0)then
            ! if line ends in backslash it is assumed a continued line
            if(line(last:last).eq.'\')then
               ! remove backslash
               line=line(:last-1)
               ! continue on and read next line and append to result
               cycle INFINITE
            endif
         endif
         ! hitting end of record is not an error for this routine
         ier=0
         ! end of reading line
         exit INFINITE
     ! end of file or error
     elseif(ier.ne.0)then
        exit INFINITE
     endif
   enddo INFINITE

   line=trim(line)

end subroutine readline

end program demo_readline

category: code