Fortran读取文本文件

Fortran读取文本文件,fortran,readfile,intel-fortran,Fortran,Readfile,Intel Fortran,我有一个这样写的文件 N 1000 NNODES 3 TURB_INT 0.20000 U_MEAN 30 TYPE_GP dav L 120 WIND_TURB NODE_1 NODE_2 NODE_3

我有一个这样写的文件

N
                1000
NNODES
                   3
TURB_INT
             0.20000
U_MEAN
                  30
TYPE_GP
                 dav
L
                 120
WIND_TURB
              NODE_1              NODE_2              NODE_3

             0.90139            -1.02858             0.03962
            -2.56887            -1.59726            -0.82062
            -0.58745             0.72129            -1.90712
            -4.46302            -2.49995            -5.45345
            -4.10550            -5.50565            -7.77285
            -6.18588            -6.34998            -5.95054
我真的很难理解如何阅读它

最后,我最需要的是将节点记录读入变量WT(I,:),因此我可以使用DO循环

以下是我所拥有的:

! open inputfile, ID=100
    open(unit=100, file=inputfile, recl=fileln, status='old', iostat=iost)

    print *, iost
    print *, fileln

    if (iost .ne. 0) then 
        print *, 'Error opening file'
        print *, erromsg
    else
        print *, 'File opened correctly'
        print *, erromsg
    end if


    ! Trying to read what's written in file.
    read(unit=100, *) N
    print *, N

    read(unit=100, *) Nval
    print *, Nval
我试着一行一行地看阅读是如何完成的。我可以读取变量N,但就在第二次读取时(第2行),我有“严重(24):读取期间文件结束”

请你给我提个建议好吗?
谢谢

我认为你应该仔细阅读你的公开声明的正确标志。 关键字
recl
用于直接访问文件。这意味着每个记录都有相同的长度,因此如果需要第64条记录,程序会立即知道该记录的位置

如果你的文件有记录长度,我看不到。正如我在评论中所说,我强烈怀疑
fileln
是以字节为单位的文件长度(您没有说)。因此,在第一次读取时,将读取第一条记录,这是整个文件,然后将其解析为变量
N
——该变量只能接受一个整数,其他所有内容都将被丢弃

然后它尝试读取文件结尾后的下一个
fileln
字节,这会导致“文件结尾”错误

在您的情况下,我将在文件
open
语句中添加
action=“READ”
form=“FORMATTED”
关键字。我总是发现,不仅要查找错误代码,还要查找错误消息
iomsg

值得一提的是,以下是我对如何阅读该文件的建议:

program readfile
    implicit none
    integer :: u  ! File handle
    integer :: ios
    character(len=100) :: iom, line
    integer :: N, NNodes, U_Mean, L
    real :: Turb_Int
    character(len=20) :: Type_GP
    real, allocatable :: node_values(:)
    character(len=10), allocatable :: node_names(:)
    character(len=*), parameter :: inputfile = 'data.txt'

    ! newunit, instead of unit, creates a new unique
    ! file unit number. You could also set u to 100
    ! and then use unit=u
    open(newunit=u, file=inputfile, action='READ', &
        status='OLD', form='FORMATTED', iostat=ios, &
        iomsg=iom)

    if (ios /= 0) then
        print *, "Error opening file:"
        print *, iom
        stop 1
    end if

    ! Read the header. I'm always reading 'line' when
    ! I expect there to be just the next keyword
    ! in the line. You might want to check for that
    read(u, *) line   ! Hopefully 'N'
    read(u, *) N
    read(u, *) line   ! Hopefully "NNODES"
    read(u, *) NNodes
    ! I assume that NNODES stands for the number
    ! of columns later in the file.
    ! So here I'm also allocating the arrays.
    allocate(node_values(NNodes))
    allocate(node_names(NNodes))
    read(u, *) line   ! Hopefully TURB_INT
    read(u, *) Turb_Int
    read(u, *) line   ! Hopefully U_MEN
    read(u, *) U_Mean
    read(u, *) line   ! Hopefully TYPE_GP
    read(u, *) Type_GP
    read(u, *) line   ! Hopefully L
    read(u, *) L
    read(u, *) line   ! Hopefully WIND_TURB
    read(u, *) node_names

    ! Just print out what we got from the header so far to see
    ! everyting's right
    write(*, '(A, I0)') "N = ", N
    write(*, '(A, I0)') "NNodes = ", NNodes
    write(*, '(A, F5.1)') "Turb_Int = ", Turb_Int
    write(*, '(A, I0)') "U_Mean = ", U_Mean
    write(*, '(A, A)') "Type_GP = ", trim(Type_GP)
    write(*, '(A, I0)') "L = ", L
    write(*, '(A, *(A))') node_names

    ! Now read the data itself. In this case I'm just reading it
    ! line by line, print it out, and forget it again
    ! until the end of the file.
    data_loop : do
        read(u, *, iostat=ios, iomsg=iom) node_values
        if (ios /= 0) exit data_loop
        print *, node_values
    end do data_loop
end program readfile

你的
文件是什么?那是文件大小吗?这就解释了它在第一次读取时读取整个文件(只存储可以放入
N
)然后在第二次读取时位于文件末尾。@Javiergaria你确定
recl
只处理未格式化的输出吗。我的理解是,您也可以将其用于格式化,只是所有记录的长度必须相同。非常感谢您为我写下了这段代码!我试着编译并执行,结果是:“forrtl:severe(24):读取过程中文件结束,单元129,文件C:\Users\Michele-Esposito\Lavoro\Bispectrum\Simulations\1000_500\2\windturb.dat”同样,设置
打开(单元=100,文件=inputfile,动作=read',状态=old',形式='format',iostat iost=iomsg=iom)
,我得到了这个错误:
error#6347:A*说明符此时在控制列表中无效。
我没有时间调试您的程序。我建议您首先尝试在我编写代码时运行我的代码(当然,您必须更改
inputfile
参数),这样应该可以运行。然后,当你根据自己的需要修改代码时,分小步看看有什么变化,什么时候会崩溃。也许我知道问题出在哪里了。它位于第二行开头的空白处,这导致程序将它们解释为文件的结尾。因为我试着用节点记录构建一个类似的文件,它成功了。然后,格式化同一个文件,但以这样一种方式记录没有左对齐,我得到了相同的错误。那么,当一行以空格开头时,如何避免这个错误(文件结尾)?非常感谢。