问题描述
我希望输出为二进制格式的矩阵,但我一直将其作为单个长数组获取.谁能建议我要去哪里错了
I want the output as a matrix in a binary format but I keep getting it as a single long array. Can anyone suggest where I am going wrong
program ascii_to_binary
implicit none
c
c declarations
integer iw,jy,i
real A(47,52),z(47,52),undef
real x(47,52)
logical exist
c----------------------------------------------------
c read in the index, insert name of file in input_file
undef = -9999.
c read in the index, insert name of file in input_file
inquire(file="weekly_mean_sce.txt", exist=exist)
if (exist) then
print *,"Exist"
open(43,file='weekly_mean_sce.txt',status='old')
do jy=1,47
read(43,*) A(jy,:)
enddo
print *, maxval(A)
write(6,*)'read in input data'
else
print *,"not there"
end if
x=reshape(A,(/47,52/))
OPEN(UNIT=15, FILE="sce.dat",ACTION="write")
do i=1,47
write(15, '(F9.2)')( real(x(i,iw)) ,iw=1,52)
end do
write(15,"(F9.2)") x(1:47,1:52)
END PROGRAM ascii_to_binary
推荐答案
常规Fortran I/O使用记录结构.如果您希望在保持记录结构的同时进行无格式的I/O操作,我建议
Normal Fortran I/O uses a record structure. If you want unformatted I/O while keeping the record structure, I would recommend
open(15,file="sce.dat", form="unformatted")
do iw=1,52 ! Better to use named constants here
write (15) x(:,iw)
end do
这将以自然的Fortran数组顺序写出值,其中存储顺序为a(1,1),a(2,1)等,外加一些其他信息(记录标记)以标识记录的起始位置和它在哪里结束.
This writes out the values in the natural Fortran array order, where the storage order is a(1,1), a(2,1) etc, plus some additional information (record markers) to identify where a record starts and where it ends.
如果不需要记录结构,则可以使用
If you do not need the record structure, you can just use
open(15,file="sce.dat",form="unformatted",access="stream")
write (15) x
这篇关于在fortran中以矩阵形式输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!