I want to be able to successively write large arrays to a single binary file in Fortran. A simple example is below:
program TEST_IO
implicit none
!Define float precision
integer, parameter :: dp = selected_real_kind(33,4931)
!Define array dimensions
integer(kind=dp) :: nrows = 1d4, ncols = 12
!Define write/read arrays
real(kind=dp), dimension(:,:), allocatable :: ArrayWrite, ArrayRead
!Allocate
allocate(ArrayWrite(nrows,ncols))
allocate(ArrayRead(2*nrows,ncols))
!Populate the array
ArrayWrite=1.0_dp
!Write to file
open(unit=10, file='example.dat' , form='unformatted')
write(10) ArrayWrite
close(10)
!Re-populate array
ArrayWrite=2.0_dp
!And append to existing file
open(unit=10, file='example.dat', form='unformatted',position='append')
write(10) ArrayWrite
close(10)
!Now read in all the data
open(unit=10, file='example.dat' , form='unformatted')
read(10) ArrayRead
close(10)
end program TEST_IO
Since I have written an array of size (nrows,ncols)
to file twice, I imagine the total write file to be of size (2*nrows,ncols)
. Indeed, from inspecting the output file size this seems to be true.
But when I try to read back in this data to ArrayRead
, I simply get a runtime error: 'I/O past end of record on unformatted file'
Can anyone provide some guidance on where I am going wrong? Thanks.