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

Binary Files

tlillys
Beginner
413 Views
Is it possible to rewind and over-write a record in sequential access binary file within the same program in which it is created?

The program opens a file with:

OPEN(UNIT=FID,
file = FileName,
Status='REPLACE',
Access='Sequential',
Form='binary',
IOSTAT=IERR)

The first record written to the file is the number of additional records that the program intends to write. It is possible that during the course of processing, the actual number of records written is less than the number intended.

In this instance, I am trying to update the first record with the correct number. The rewind statement doesn't return an error and neither does the write. However, after closing and opening the file with another program, the original value in the first record persists, the update is not apparent.
Any takers?

Thanks
0 Kudos
2 Replies
dave_frank
Beginner
413 Views
rewinding a file and then writing a record effectively deletes all previous data that existed or was just written to a sequential file.

you could close the file perserving your updates, then
open(1,file='test.dat',form='binary',access='direct',recl=4)

read (1,rec=1) oldcount
newcount = oldcount+?
write (1,rec=1) newcount
close (1)
0 Kudos
Steven_L_Intel1
Employee
413 Views
If you use direct access (which requires fixed-length records), you can rewrite individual records. As Dave says, if you use sequential writes, all data following in the file becomes undefined.
0 Kudos
Reply