简体   繁体   English

我如何在fortran的do-loop的每个步骤中读入文本文件的连续行?

[英]How do I read in a consecutive row of a text file in each step of a do-loop in fortran?

I have a dataset of parameter values for 30 species, and I want to run a script that conducts a simulation for each species. 我有一个30种物种参数值的数据集,我想运行一个脚本来对每种物种进行仿真。 The parameter values are currently stored in a .txt file, where each row is a different species, and each column is a different parameter value. 参数值当前存储在.txt文件中,其中每一行是不同的种类,每列是不同的参数值。 What I'd like to do is set up a do-loop that reads in the relevant row of parameter values for each species, runs the simulation script, and writes a .txt file of the output for each species. 我想做的是设置一个do循环,该循环读取每个物种的相关参数值行,运行模拟脚本,并为每个物种写入输出的.txt文件。 Unfortunately, I'm new to fortran and having a lot of trouble understanding how to read in consecutive rows from a .txt file in each step of a do loop. 不幸的是,我是fortran的新手,在执行do循环的每个步骤中如何从.txt文件读取连续的行时遇到很多麻烦。 I tried making a simplified script to test whether the read step was working: 我尝试制作一个简化的脚本来测试读取步骤是否正常工作:

    PROGRAM DRIVER
    IMPLICIT NONE

    INTEGER :: mm ! I forgot this line in the first version of this question   
    and edited to add it in
    CHARACTER(7) :: species  !! the first column is the species name
    REAL*8    :: leaf_variable   !  The next 3 columns are variable values
    REAL*8    :: stem_variable   !  
    REAL*8    :: root_variable   !  

    OPEN (12, file = "species_parameters.txt") ! open the .txt file

    DO mm = 1,30 ! set up the do loop
        READ (12,*) species, leaf_variable, stem_variable, root_variable
        ! Read in the species-specific parameter values
        WRITE (*,*) species, leaf_variable, stem_variable, root_variable
        ! Print the values on the screen just to show the do loop runs
    ENDDO
    END PROGRAM DRIVER

But when I go to compile, I get the error: At line XX of file XX (unit = 12, file = 'species_parameters.txt') Fortran runtime error: End of file 但是当我进行编译时,出现错误:在文件XX的第XX行(单位= 12,文件='species_parameters.txt')Fortran运行时错误:文件结尾

What am I misunderstanding about opening and reading in this file? 关于打开和阅读此文件,我有什么误解?

Thanks very much for any help. 非常感谢您的帮助。

EDIT: I think I've narrowed down my problem. 编辑:我想我已经缩小了我的问题。 My understanding is that read() takes in one row in a .txt file at a time, so that in this example: 我的理解是read()一次在.txt文件中占用一行,因此在此示例中:

    read(7, *) species, leaf_variable, stem_variable, root_variable
    read(7, *) species, leaf_variable, stem_variable, root_variable

The variables should equal the values in the second row of the .txt file. 变量应等于.txt文件第二行中的值。 Instead, no matter how many times I put in the read() function, the variable values equal the first row. 相反,无论我将多少次放入read()函数,变量值都等于第一行。 And, even though there are only 4 columns, I can define as many variables as I want with a read() function: 而且,即使只有4列,我也可以使用read()函数定义尽可能多的变量:

   read(7, *) species, leaf_variable, stem_variable, root_variable, 
            fake_variable1, fake_variable2, fake_variable3, fake_variable4

where the fake_variable values equal the values in the second row of the .txt file. 其中fake_variable值等于.txt文件第二行中的值。 Am I confused about what read() does, or is there something I need to do to keep my script from reading my entire .txt file as one line? 我是否对read()的功能感到困惑,还是需要做一些事情来防止脚本将我的整个.txt文件读为一行?

EDIT #2: The do loop reads line by line correctly now that I've saved my .txt file with Unix encoding using TextWrangler. 编辑#2:既然我已经使用TextWrangler以Unix编码保存了.txt文件,那么do循环会正确地逐行读取。 The original file was saved as a .txt file with Excel. 原始文件已用Excel保存为.txt文件。 This seems to have solved it, but if anyone has suggestions for a better way to specify the input file format, I'd appreciate it. 这似乎已经解决了,但是如果有人对建议指定输入文件格式有更好的建议,我将不胜感激。 The first few lines of the input file look like this: 输入文件的前几行如下所示:

    species1,1.2,6.54,10.9
    species2,1.42,3.5,8.23
    species3,0.85,2.41,4.9 

A run time error is when you have an executable, execute it, and it crashes. 运行时错误是当您拥有可执行文件时,执行该错误,然后崩溃。 A compile time error is when the compiler fails to produce an executable. 编译时错误是指编译器无法生成可执行文件。

This code shouldn't compile, because you have IMPLICIT NONE , but haven't declared the integer mm . 该代码不应编译,因为您具有IMPLICIT NONE ,但尚未声明整数mm

What I'd recommend is to get more information: 我建议您获取更多信息:

program driver
    use iso_fortran_env
    implicit none
    character(len=7) :: species
    real(kind=real64) :: leaf_variable, stem_variable, root_variable
    integer :: u, ioerr
    character(len=120) :: iomsg

    open(newunit=u, file='species_parameters.txt', action='read', status='old', iostat=ioerr, iomsg=iomsg)
    if (ioerr /= 0) then
        print *, "Error opening file"
        print *, trim(iomsg)
        stop 1
    end if
    do
        read(u, *, iostat=ioerr, iomsg=iomsg) species, leaf_variable, stem_variable, root_variable
        if (ioerr /= 0) exit  ! exits the loop
        write(*, *) species, leaf_variable, stem_variable, root_variable
    end do
    print *, trim(iomsg)
    close(u)
end program driver

This will always print the "read past end of file" error, but this is just to check how to program reads anyway. 这将始终显示“读取文件末尾”错误,但这只是为了检查如何对程序进行读取。

This should compile, and when you run it, it should give you some information on what is going wrong. 它应该编译,并且在您运行它时,它应该为您提供一些有关出问题的信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM