Read a single line from a file.
Type | Intent | Optional | Attributes | Name | ||
---|---|---|---|---|---|---|
integer, | intent(in) | :: | iunit |
the file unit (assumed to be opened) |
||
character(len=:), | intent(out), | allocatable | :: | line |
the line read |
|
logical, | intent(out) | :: | eof |
if end of file reached |
subroutine read_line_from_file(iunit,line,eof) implicit none integer,intent(in) :: iunit !! the file unit (assumed to be opened) character(len=:),allocatable,intent(out) :: line !! the line read logical,intent(out) :: eof !! if end of file reached integer,parameter :: buffer_size = 256 !! the size of the read buffer [arbitrary] integer :: nread !! character count specifier for read statement integer :: istat !! file read `iostat` flag character(len=buffer_size) :: buffer !! the file read buffer nread = 0 buffer = '' line = '' eof = .false. do ! read in the next block of text from the line: read(iunit,fmt='(A)',advance='NO',size=nread,iostat=istat) buffer if (IS_IOSTAT_END(istat)) then ! add the last block of text before the end of the file if (nread>0) line = line//buffer(1:nread) eof = .true. exit elseif (IS_IOSTAT_EOR(istat)) then ! add the last block of text before the end of record if (nread>0) line = line//buffer(1:nread) exit elseif (istat==0) then ! all the characters were read line = line//buffer ! add this block of text to the string else ! some kind of error write(*,*) 'istat=',istat error stop 'Read error.' end if end do end subroutine read_line_from_file