バイナリ ファイルを fortran プログラムに渡すシェル スクリプトがあります。
Mth=$1
loop=1
it=1
while test $it -le 12
do
Mth=`expr $Mth + $loop`
file="DataFile"$Mth".bin"
./fort_exe ${Yr} ${nt} ${it}
# Increment loop
it=`expr $it + 1`
done
このスクリプトは、do ループ内の 12 個のファイルを fortran プログラムに渡すために使用されます。Fortran プログラムでは、シェル スクリプトから渡されたバイナリ ファイルを読み取り、連続したファイルから読み取られたすべてのデータを 1 つのファイルにコンパイルする 2 番目のファイルを書き込もうとしています。
!Open binary file passed from shell script
open(1,file='Datafile'//TRIM{Mth)//.bin',action='read',form='unformatted',access='direct', &
recl=4*x*y, status='old')
! Open write file for t 1. The status is different in t 1 and t > 1 so I open it twice: I guess there is a more elegant way to do this...
open(2,file='Newfile.bin',action='write',form='unformatted', &
access='stream', position='append', status='replace')
irec = 0
do t = 1, nt
! Read input file
irec = irec + 1
read(1,rec=irec) val(:,:)
! write output file
irecW= irec + (imonth-1)*nt
if ( t .eq. 1) write(2,pos=irecW) val(:,:)
! Close file after t = 1, update the status to old and reopen.
if ( t .eq. 2) then
close (2)
open(2,file='Newfile.bin',action='write',form='unformatted', &
access='stream', position='append',status='old')
endif
if ( t .ge. 2) write(2,pos=irecW) val(:,:)
enddo
最初のファイルからバイナリデータを問題なく読み取ることができますが、最初のプログラムで書き込んだファイルからバイナリデータを別のプログラムから読み取ろうとすると、
open(1,file='Newfile.bin',action='read',form='unformatted', &
access='stream', status='old')
irec=0
do t = 1, nt
! Read input file
irec = irec + 1
read(1,pos=irec) val(:,:)
write(*,*) val(:,:)
enddo
val(:,:) はゼロのリストに他なりません。これは、私が position='append' を使用できる唯一の方法であると信じている access=stream を使用するのは初めてです。gfortran と ifort でコンパイルしようとしましたが、エラー メッセージは表示されません。
なぜこれが起こっているのか誰にも分かりますか?