从文本读取并写入二进制

时间:2019-09-04 11:51:25

标签: fortran

  INTEGER,PARAMETER :: nlv=6
   INTEGER,DIMENSION(nlv) :: aa,bb,cc,dd,ee,ff,gg
   INTEGER::rec,irec

OPEN(50,FILE = test.txt',FORM ='formatted',ACCESS ='sequential',STATUS ='old',ACTION ='READ')

OPEN(60,FILE ='test.bin',FORM ='unformatted',ACCESS ='direct',STATUS ='unknown',recl = 6 * 4,ACTION ='WRITE')

DO K = 1,6

  READ(50,76,err=97)aa(k),bb(k),cc(k),dd(k),ee(k),ff(k),gg(k)

76 FORMAT(i2,1x,i5,1x,i6,1x,i5,1x,i5,1x,i5,1x,i5)

  END DO


  WRITE(60,rec=irec)dd

  irec=irec+1

97读取数据时发现WRITE(* *)错误

  WRITE(*,*)"End of file found"

  CLOSE (50)

1 个答案:

答案 0 :(得分:0)

我猜想当您使用直接访问时,记录的大小必须与正在写入的数据大小匹配或更大。您声称记录长度= 6(以字节为单位?),但是您正在写入6 * 4bytes = 24bytes。