爱气象,爱气象家园! 

气象家园

 找回密码
 立即注册

QQ登录

只需一步,快速开始

新浪微博登陆

只需一步, 快速开始

搜索
查看: 5094|回复: 4

[求助] 请教Fortran中有关二进制direct文件追加写入的问题

[复制链接]

新浪微博达人勋

发表于 2015-2-11 20:14:49 | 显示全部楼层 |阅读模式

登录后查看更多精彩内容~

您需要 登录 才可以下载或查看,没有帐号?立即注册 新浪微博登陆

x
本帖最后由 wheelar 于 2015-2-11 20:50 编辑

我想追加数据写入二进制直接文件中,就是反复多次open,close同一个二进制文件,我试了position=‘append’选项,但是失败了,可是同样的方法在文本文件顺序文件却正确,不知哪位大侠可以给解释一下?以下是代码,编译器是IVF11

program main
    implicit none
    integer,parameter :: intx=4, inty=5, intv=6
    real :: odata(intx,inty)
    integer :: iv
    do iv=1,intv
        odata=iv       ! 把odata这个数组的所有值设为iv
        call sub_writeFile(odata,intx,inty,'out.txt') !追加数据到文本文件中,正确
!        call sub_appendFileBinary(odata,intx,inty,'out.grd') !追加数据到binary中,结果出错了!
    enddo
    read(*,*)
end program main
   
    subroutine sub_writeFile(odata,intx,inty,strfnout)  !追加数据到文本文件中
        integer,intent(in) :: intx,inty
        real,intent(in) :: odata(intx,inty)
        character(len=*),intent(in)  :: strfnout
        integer :: i,j
        open(unit=12,file=strfnout,access='sequential',status='unknown',position='append',action='write')
        endfile(unit=12)
        write(12,*)'--------------------------------------------------------------'
        do i=1,intx
            write(12,'(5f10.2)')(odata(i,j),j=1,inty)
        enddo
        close(unit=12)
    end subroutine sub_writeFile
   
    subroutine sub_appendFileBinary(odata,intx,inty,strfnout) !追加数据到binary中
        integer,intent(in) :: intx,inty
        real,intent(in) :: odata(intx,inty)
        character(len=*),intent(in)  :: strfnout
        
        integer :: i,j
        open(unit=12,file=strfnout,access='direct',recl=intx*inty,position='append',&
                    status='unknown',action='write')
        endfile(unit=12)
        write(12)((odata(i,j),i=1,intx),j=1,inty)
        close(unit=12)
    end subroutine sub_appendFileBinary


我写了两个subroutine,sub_writeFile和sub_appendFileBinary,几乎就同样的方法,第一个是追加写入文本文件,第二个是binary文件,可是执行第二个subroutine时,在open语句那里就出问题了(红色代码),甚至还没执行到endfile(unit=12)。提示是:“ ifort (46)inconsistent OPEN/CLOSE parameters”, 网上查询的结果是:
EXPLANATION: Specifications in an OPEN or CLOSE statement were inconsistent. Some invalid combinations follow:
- READONLY with STATUS='NEW' or STATUS='SCRATCH'
- ACCESS='APPEND' with READONLY, STATUS='NEW' or STATUS='SCRATCH'
- DISPOSE='SAVE', 'PRINT', or 'SUBMIT' with STATUS='SCRATCH'
- DISPOSE='DELETE' with READONLY
o MESSAGE: Inconsistent record length
可是我也没有用status=‘new’或readonly等东东啊?我想问问这个究竟该怎么写呢?
另外,我看有点地方说是position='append',还有说是,access='append',哪个正确呢?

最后,我试验了 open(unit=12,file=strfnout,access='sequential’,form='unformatted',position='append',&
status='unknown',action='write'),
结果可以通过执行,那么是不是说追加position='append'一定要和access='sequential’连用呢



密码修改失败请联系微信:mofangbao

新浪微博达人勋

发表于 2015-2-12 00:16:33 来自手机 | 显示全部楼层
没有试过,明天实践一下
密码修改失败请联系微信:mofangbao

新浪微博达人勋

 楼主| 发表于 2015-2-12 10:03:26 | 显示全部楼层
虽然sequential可以,但是会多输出一些东西,例如打开一次文件就输入5x4的数组,是20X4=80字节,输入6次就是480字节,但实际是文件却是528字节,我也实际看了一下文件内容,发现顺序输入,总在首尾各多出4字节的东西,这样相当于每次输入22个数字了,以下是从生成的binary文件重新读出的结果,左边编号是文件第几个数据,右面是具体的数值
           1  1.1210388E-43
           2   1.000000   
           3   1.000000   
           4   1.000000   
           5   1.000000   
           6   1.000000   
           7   1.000000   
           8   1.000000   
           9   1.000000   
          10   1.000000   
          11   1.000000   
          12   1.000000   
          13   1.000000   
          14   1.000000   
          15   1.000000   
          16   1.000000   
          17   1.000000   
          18   1.000000   
          19   1.000000   
          20   1.000000   
          21   1.000000   
          22  1.1210388E-43
          23  1.1210388E-43
          24   2.000000   
          25   2.000000   
          26   2.000000   
          27   2.000000   
          28   2.000000   
          29   2.000000   
          30   2.000000   
          31   2.000000   
          32   2.000000   
          33   2.000000   
          34   2.000000   
          35   2.000000   
          36   2.000000   
          37   2.000000   
          38   2.000000   
          39   2.000000   
          40   2.000000   
          41   2.000000   
          42   2.000000   
          43   2.000000   
          44  1.1210388E-43
          45  1.1210388E-43
          46   3.000000   
          47   3.000000   
          48   3.000000   
          49   3.000000   
          50   3.000000   
          51   3.000000   
          52   3.000000   
          53   3.000000   
          54   3.000000   
          55   3.000000   
          56   3.000000   
          57   3.000000   
          58   3.000000   
          59   3.000000   
          60   3.000000   
          61   3.000000   
          62   3.000000   
          63   3.000000   
          64   3.000000   
          65   3.000000   
          66  1.1210388E-43
          67  1.1210388E-43
          68   4.000000   
          69   4.000000   
          70   4.000000   
          71   4.000000   
          72   4.000000   
          73   4.000000   
          74   4.000000   
          75   4.000000   
          76   4.000000   
          77   4.000000   
          78   4.000000   
          79   4.000000   
          80   4.000000   
          81   4.000000   
          82   4.000000   
          83   4.000000   
          84   4.000000   
          85   4.000000   
          86   4.000000   
          87   4.000000   
          88  1.1210388E-43
          89  1.1210388E-43
          90   5.000000   
          91   5.000000   
          92   5.000000   
          93   5.000000   
          94   5.000000   
          95   5.000000   
          96   5.000000   
          97   5.000000   
          98   5.000000   
          99   5.000000   
         100   5.000000   
         101   5.000000   
         102   5.000000   
         103   5.000000   
         104   5.000000   
         105   5.000000   
         106   5.000000   
         107   5.000000   
         108   5.000000   
         109   5.000000   
         110  1.1210388E-43
         111  1.1210388E-43
         112   6.000000   
         113   6.000000   
         114   6.000000   
         115   6.000000   
         116   6.000000   
         117   6.000000   
         118   6.000000   
         119   6.000000   
         120   6.000000   
         121   6.000000   
         122   6.000000   
         123   6.000000   
         124   6.000000   
         125   6.000000   
         126   6.000000   
         127   6.000000   
         128   6.000000   
         129   6.000000   
         130   6.000000   
         131   6.000000   
         132  1.1210388E-43

密码修改失败请联系微信:mofangbao

新浪微博达人勋

发表于 2015-2-12 14:42:44 | 显示全部楼层
刚写程式的时候也常遇到这种问题。上面答的好,sequential 不等于 direct access。看档案大小就知道 sequential 多了点东西。。。还可能有 big endian little endian 的问题,以前没人带没人问,搞得很烦。
密码修改失败请联系微信:mofangbao

新浪微博达人勋

 楼主| 发表于 2015-2-13 19:11:49 | 显示全部楼层
找到解决方法了,用open(unit=12,file=strfnout,access='sequential',form='binary',position='append',action='write')可以解决问题,在百度上一位仁兄解答的,可惜彭国伦的书里并没有提form后面的关键字还可以用binary,以为只可以是formatted,unformatted两个
binary和unformatted的区别:
An unformatted file is a sequence of unformatted records. An unformatted record is a sequence of values. Unformatted direct files contain only this data, and each record is padded to a fixed length with undefined bytes. Unformatted sequential files contain the data plus information that indicates the boundaries of each record.

Binary sequential files are sequences of bytes with no internal structure. There are no records. The file contains only the information specified as I/O list items in WRITE statements referring to the file.
也就是说unformatted文件不仅仅只是数值,还包含 information that indicates the boundaries of each record(每个记录的边界)。而binary则只含有数值。
密码修改失败请联系微信:mofangbao
您需要登录后才可以回帖 登录 | 立即注册 新浪微博登陆

本版积分规则

Copyright ©2011-2014 bbs.06climate.com All Rights Reserved.  Powered by Discuz! (京ICP-10201084)

本站信息均由会员发表,不代表气象家园立场,禁止在本站发表与国家法律相抵触言论

快速回复 返回顶部 返回列表