如何解决不正确的fortran从数据文本文件读取? [英] How to resolve incorrect fortran read from data text file?

查看:953
本文介绍了如何解决不正确的fortran从数据文本文件读取?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  108 6.7522 

我有一个Fortran子程序READCN,它将文本文件中的数字108 6.7522存储到变量NN和BOX中

  SUBROUTINE READCN(CNFILE,BOX)
CHARACTER CNFILE *(*)
REAL BOX
INTEGER CNUNIT
PARAMETER (CNUNIT = 10)
INTEGER NN
OPEN(UNIT = CNUNIT,FILE = CNFILE,STATUS ='OLD',
:FORM ='UNFORMATTED')

READ(CNUNIT)NN,BOX
WRITE(*,*)NN,BOX $ b $ CLOSE(UNIT = CNUNIT)
RETURN
END

READCN变量NN BOX的输出是

  3.2997999 2.74554597E-31 

如何正确读取值? b

这个程序取自 http://www.ccl.net/cca/software/SOURCES/FORTRAN/allen-tildesley-book/f。 12



当我试图删除读命令中的格式选项时,这是我收到的跟随错误

 在文件MCNPT.f的第686行(单元= 10,file ='LATTICE-256.txt')
Fortran运行时错误:FORMATTED数据传输格式丢失


解决方案

从使用无格式输入转换为格式化输入需要三个操作:


  • 将输入更改为文本文件;
  • 打开文件进行格式化,而不是未格式化的I / O;
  • 提供读取语句的格式。
  • 你已经完成了前两个。 [如果您不知道,删除 form ='unformatted'表示使用默认的格式化I / O。]



    剩下的就是在随后的 read s中使用格式说明符。这是编译器在抱怨的地方。

    最简单的格式化读取是 list-directed ,它由 中的> fmt = * 说明符读取。这应该足够你的情况,但你应该确保你对它的限制感到满意。



    确切地说:替换

      OPEN(UNIT = CNUNIT,FILE = CNFILE,STATUS ='OLD',
    :FORM ='UNFORMATTED')
    READ(CNUNIT)NN, BOX

    with

      OPEN(UNIT = CNUNIT,FILE = CNFILE,STATUS ='OLD')
    READ(CNUNIT,*)NN,BOX


    I have a text file with two numbers on the same line

    108  6.7522
    

    I have a fortran subroutine READCN that stores the numbers 108 6.7522 from the text file into the variables NN and BOX

        SUBROUTINE READCN ( CNFILE, BOX )
        CHARACTER   CNFILE*(*)
        REAL        BOX
        INTEGER     CNUNIT
        PARAMETER ( CNUNIT = 10 )
        INTEGER     NN
        OPEN ( UNIT = CNUNIT, FILE = CNFILE, STATUS = 'OLD',
     :         FORM = 'UNFORMATTED'                        )
    
        READ ( CNUNIT ) NN, BOX
        WRITE(*,*) NN, BOX
        CLOSE ( UNIT = CNUNIT )
        RETURN
        END
    

    The output for READCN variables NN BOX is

       3.2997999      2.74554597E-31
    

    How do I read the values correctly?

    This program is taken from http://www.ccl.net/cca/software/SOURCES/FORTRAN/allen-tildesley-book/f.12

    When I attempted to remove the format option in the read command this was the follow error I received

    At line 686 of file MCNPT.f (unit = 10, file = 'LATTICE-256.txt')
    Fortran runtime error: Missing format for FORMATTED data transfer
    

    解决方案

    Moving from using unformatted input to formatted input requires three actions:

    • changing the input to a "text file";
    • opening the file for formatted, rather than unformatted, I/O;
    • providing a format for the read statements.

    You've done the first two of these. [If you didn't know, removing form='unformatted' means that the default of formatted I/O is used.]

    All that remains is to use a format specifier in the subsequent reads. This is where the compiler is complaining.

    The simplest formatted read to use is list-directed, which is given by fmt=* specifier in the read. This should be sufficient for your case, but you should ensure you are happy with its limitations.

    To be precise: replace

          OPEN ( UNIT = CNUNIT, FILE = CNFILE, STATUS = 'OLD',
         :         FORM = 'UNFORMATTED'                        )
          READ ( CNUNIT ) NN, BOX
    

    with

          OPEN ( UNIT = CNUNIT, FILE = CNFILE, STATUS = 'OLD')
          READ ( CNUNIT, * ) NN, BOX
    

    这篇关于如何解决不正确的fortran从数据文本文件读取?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆