I’m trying to read a binary file in FORTRAN 77 with ACCESS='DIRECT'
, but unfortunately, without success. The example function opens a file in direct mode/unformatted, and tries to read all data byte by byte (in this case, each byte just into character A
):
CHARACTER A
CHARACTER*32 MSG
INTEGER ISTAT, NBYTES
NBYTES = 1024
OPEN (UNIT=10, ACCESS='DIRECT', ACTION='READ', FILE=PATH,
& FORM='UNFORMATTED', IOSTAT=ISTAT, RECL=NBYTES,
& STATUS='OLD')
IF (ISTAT .NE. 0) RETURN
DO 10 I = 1, NBYTES
READ (10, IOSTAT=ISTAT, IOMSG=MSG, REC=I) A
IF (ISTAT .NE. 0) THEN
PRINT *, ISTAT, MSG
CLOSE (10)
RETURN
END IF
10 CONTINUE
CLOSE (10)
The file is 1024 bytes in size. The first byte is read successfully, but the second one always fails with error “non-existing record number” (5002).
Using ACCESS='STREAM'
instead, which is not a FORTRAN 77 feature, it works:
OPEN (UNIT=10, ACCESS='STREAM', ACTION='READ', FILE=PATH,
& FORM='UNFORMATTED', IOSTAT=ISTAT, STATUS='OLD')
IF (ISTAT .NE. 0) RETURN
DO 10 I = 1, NBYTES
READ (10, IOSTAT=ISTAT, IOMSG=MSG) A
IF (ISTAT .NE. 0) THEN
PRINT *, ISTAT, MSG, I
CLOSE (10)
RETURN
END IF
10 CONTINUE
Does someone know what I’m doing wrong?