Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.

End of record during read, using non-advancing read

OP1
New Contributor II
838 Views

The code below triggers the runtime error: "forrtl: severe (268): end of record during read, unit 20, file .....". Lines 11 and 13 do not produce the same outcome, is this expected?

PROGRAM P
IMPLICIT NONE
INTEGER :: LINE_SIZE
CHARACTER(LEN=15) :: BUFFER
OPEN(UNIT=20,FILE='test.txt',STATUS='REPLACE')
WRITE(20,FMT='(A)') '1234567890'
WRITE(20,FMT='(A)') 'abcdefghij'
WRITE(20,FMT='(A)') ')(@&%*((@!'
CLOSE(20)
OPEN(UNIT=20,FILE='test.txt',STATUS='OLD')
READ(20,FMT='(A)') BUFFER
REWIND(20)
READ(20,FMT='(A)',ADVANCE='NO',SIZE=LINE_SIZE) BUFFER
CLOSE(20)
END PROGRAM P

 

0 Kudos
2 Replies
OP1
New Contributor II
838 Views

ok - there is probably no problem here. The modified code below works as intended, maybe this will be helpful to somebody. One simply needs to guard against the EOR condition.

PROGRAM P
USE ISO_FORTRAN_ENV,ONLY : IOSTAT_EOR,IOSTAT_END
IMPLICIT NONE
INTEGER :: LINE_SIZE,IO_ERROR
CHARACTER(LEN=15) :: BUFFER
OPEN(UNIT=20,FILE='test.txt',STATUS='REPLACE')
WRITE(20,FMT='(A)') '1234567890'
WRITE(20,FMT='(A)') 'abcdefghij'
WRITE(20,FMT='(A)') ')(@&%*((@!'
CLOSE(20)
OPEN(UNIT=20,FILE='test.txt',STATUS='OLD')
READ(20,FMT='(A)') BUFFER
REWIND(20)
READ(20,FMT='(A)',ADVANCE='NO',SIZE=LINE_SIZE,IOSTAT=IO_ERROR) BUFFER
IF ((IO_ERROR==IOSTAT_EOR).OR.(IO_ERROR==IOSTAT_END)) IO_ERROR = 0
IF (IO_ERROR/=0) THEN
    STOP
END IF
CLOSE(20)
WRITE(*,*) LINE_SIZE
END PROGRAM P

 

0 Kudos
andrew_4619
Honored Contributor II
838 Views

A useful chap that IOSTAT I never do a read / write / open / close without one!

0 Kudos
Reply