Paul,
(new post of old problem for clean thread)
I am trying to understand what is wrong, so I created a 'simpler' example. This uses records of 256 bytes (no header/footer) and reads a single byte from a random address.
If I:
- write the records sequentially, without an address, then read randomly using pos=calculated_address appears to work.
- write the records sequentially, with a calculated address, then read randomly using pos= with the same calculated address approach, it FAILS.
- write the records randomly, with a calculated address, then read pos= with the same calculated address, it FAILS. With this random write test 3, the file appears to be truncated, possible after the last write, as I got an end of file error for a valid address in the read test with 212 records of 28 length. (1MB file)
This program example is possibly easier to follow and identify the problem(s).
Test 1 : gives hope, as the read (pos=) test appears to work, but Test 2 : write ( pos=) looks to be incorrect. Test 3 : random write could be worrying for rewriting data ? All tests appear to work with gFortran, hopefully showing my approach to stream I/O is as expected.
The calculated address: for record:byte in read > address = (record-1)*256 + byte for start of record in write > address = (record-1)*256+1 for byte read > byte value = mod (address,record_length)
https://www.dropbox.com/s/jpdu6f8ub0z9d63/stream_read.f90?dl=0
For stream I/O to be a useful approach for large files (> 4GB), pos= needs to support an 8-byte addresses.
This example above does not test Inquire, which also gives an incorrect address. The code below is taken from an earlier post.
subroutine get_stream_pos (stream_unit, stream_address)
integer*4 :: stream_unit
integer*8 :: stream_address
integer iostat
inquire ( unit=stream_unit, pos=stream_address, iostat=iostat )
write (*,*) 'stream is at', stream_address, ' iostat =',iostat
end subroutine get_stream_pos