如何使用Fortran读取.txt文件中的特殊行?

如何使用Fortran读取.txt文件中的特殊行?,fortran,fortran90,fortran77,fortran95,Fortran,Fortran90,Fortran77,Fortran95,我有一个.txt文件,下面有几行特殊内容: .... .... !INPUT_PARAMETERS 1 2 5 10 ... ... 我想看评论行后面的数字!输入参数。因此,如果我有: integer:: A,B,C,D 我想得到A=1,B=2,C=5,D=10。 我怎样才能做到这一点呢?我有一段代码,可能会很有用 !--------------------------------------------------- ! Locate file in input subroutine l

我有一个.txt文件,下面有几行特殊内容:

....
....
!INPUT_PARAMETERS
1 2 5 10
...
...
我想看评论行后面的数字!输入参数。因此,如果我有:

integer:: A,B,C,D
我想得到A=1,B=2,C=5,D=10。
我怎样才能做到这一点呢?

我有一段代码,可能会很有用

!---------------------------------------------------
! Locate file in input

subroutine locate(fileid, keyword, have_data)
    implicit none
    
    integer,intent(in)          :: fileid               ! File unit number
    character(len=*),intent(in) :: keyword              ! Input keyword 
    logical,intent(out)         :: have_data            ! Flag: input found

    character*(100)             :: linestring           ! First 100 chars
    integer                     :: keyword_length       ! Length of keyword
    integer                     :: io                   ! File status flag

    keyword_length = len(keyword)
    rewind(fileid)
    
    ! Loop until end of file or keyword found
    do
        ! Read first 100 characters of line
        read (fileid,'(a)',iostat=io) linestring

        ! If end of file is reached, exit
        if (io.ne.0) then 
            have_data = .false.
            exit
        end if
        
        ! If the first characters match keyword, exit
        if (linestring(1:keyword_length).eq.keyword) then
            have_data = .true.
            exit
        endif

    end do

end subroutine locate
这被称为:

call locate(infileid, '!INPUT_PARAMETERS', found)
if (found) then
    !You can do error checking with readin flag
    read(infileid,*, IOSTAT=readin) a, b, c, d
else
    !Set default values
    a = 0;  b = 0
    c = 0;  d = 0
endif

我有一段我用过的代码可能很有用

!---------------------------------------------------
! Locate file in input

subroutine locate(fileid, keyword, have_data)
    implicit none
    
    integer,intent(in)          :: fileid               ! File unit number
    character(len=*),intent(in) :: keyword              ! Input keyword 
    logical,intent(out)         :: have_data            ! Flag: input found

    character*(100)             :: linestring           ! First 100 chars
    integer                     :: keyword_length       ! Length of keyword
    integer                     :: io                   ! File status flag

    keyword_length = len(keyword)
    rewind(fileid)
    
    ! Loop until end of file or keyword found
    do
        ! Read first 100 characters of line
        read (fileid,'(a)',iostat=io) linestring

        ! If end of file is reached, exit
        if (io.ne.0) then 
            have_data = .false.
            exit
        end if
        
        ! If the first characters match keyword, exit
        if (linestring(1:keyword_length).eq.keyword) then
            have_data = .true.
            exit
        endif

    end do

end subroutine locate
这被称为:

call locate(infileid, '!INPUT_PARAMETERS', found)
if (found) then
    !You can do error checking with readin flag
    read(infileid,*, IOSTAT=readin) a, b, c, d
else
    !Set default values
    a = 0;  b = 0
    c = 0;  d = 0
endif

看起来你想用一个名字列表。否则,您需要编写一个解析器,将每一行读入一个字符串。当前的读取字符串用于确定下一行的处理方式。@evets建议更容易:移动到文件中下一行包含感兴趣的数字的位置,然后readunit,*a、b、c、d就可以完成这项任务。至于如何移动到正确的位置,现在是时候让您与我们分享您目前掌握的代码,以便我们了解您需要/想要多少帮助。找到正确的位置是我评论的重点,@HighPerformanceMark。假设!输入参数在文件中,紧跟在所需数据之前,只需执行readunit,'A'字符串,后跟if trimstring=='!然后输入参数。维奥拉,你找到正确的位置了。当然,问题以及为什么需要解析读取的字符串,可能是文件中的其他行很重要。您似乎想要使用名称列表。否则,您需要编写一个解析器,将每一行读入一个字符串。当前的读取字符串用于确定下一行的处理方式。@evets建议更容易:移动到文件中下一行包含感兴趣的数字的位置,然后readunit,*a、b、c、d就可以完成这项任务。至于如何移动到正确的位置,现在是时候让您与我们分享您目前掌握的代码,以便我们了解您需要/想要多少帮助。找到正确的位置是我评论的重点,@HighPerformanceMark。假设!输入参数在文件中,紧跟在所需数据之前,只需执行readunit,'A'字符串,后跟if trimstring=='!然后输入参数。维奥拉,你找到正确的位置了。当然,问题以及为什么需要解析读取的字符串,可能是文件中的其他行很重要。