首页 > 解决方案 > 将未格式化的数据附加到 Fortran 中的文件,然后读取它

问题描述

我希望能够将大型数组连续写入 Fortran 中的单个二进制文件。一个简单的例子如下:

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

由于我已经将一个大小数组写入(nrows,ncols)文件两次,我想总写入文件的大小为(2*nrows,ncols). 事实上,从检查输出文件大小来看,这似乎是真的。

ArrayRead但是,当我尝试将这些数据读回至

谁能提供一些关于我哪里出错的指导?谢谢。

标签: arraysiofortrangfortran

解决方案


推荐阅读