Community
cancel
Showing results for 
Search instead for 
Did you mean: 
Highlighted
Beginner
41 Views

End of record during read, using non-advancing read

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
Highlighted
Beginner
41 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
Highlighted
Valued Contributor III
41 Views

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

0 Kudos