Hello
I have a question about unformatted sequential file input/output.
I was hoping that I might be able to write a dummy integer to file, then
write a load of records, then use REWIND to go back to the beginning of
the file, and then overwrite the dummy integer with the number of
records written before closing the file.
However, it appears that when I try to read the data back in again, I
can't get past the first integer without hitting an end of file.
Does this mean that an end of file marker is placed at the end of
whatever is the last write statement, effectively making what I was
trying to do with a sequential file impossible?
I have appended a small program demonstrating what I trying.
Thanks,
Paul
PROGRAM test_rewind_prog
IMPLICIT NONE
INTEGER, PARAMETER :: unit=20
INTEGER :: record_1, record_2
INTEGER :: n_records
INTEGER :: dummy
! Initialise
record_1=1
record_1=2
n_records=2
dummy=0
! Write to file
OPEN(&
& UNIT=unit,&
& FILE="test_rewind_prog.dat", &
& FORM='UNFORMATTED',&
& ACCESS='SEQUENTIAL',&
& ACTION='WRITE',&
& STATUS='REPLACE'&
& )
WRITE(UNIT=unit) dummy
WRITE(UNIT=unit) record_1
WRITE(UNIT=unit) record_2
REWIND(UNIT=unit)
WRITE(UNIT=unit) n_records
CLOSE(UNIT=unit)
! Zero
record_1=0
record_1=0
n_records=0
! Read from file
OPEN(&
& UNIT=unit,&
& FILE="test_rewind_prog.dat", &
& FORM='UNFORMATTED',&
& ACCESS='SEQUENTIAL',&
& ACTION='READ',&
& STATUS='OLD'&
& )
READ(UNIT=unit) n_records
! Get an end of file error on this line
READ(UNIT=unit) record_1
READ(UNIT=unit) record_2
CLOSE(UNIT=unit)
! Print to screen
PRINT *,n_records,record_1,record_2
END PROGRAM test_rewind_prog
|