Fortran find string in txt file

前端 未结 1 1001
执笔经年
执笔经年 2021-01-29 12:38

I would like to know how to find a string in a txt file and read the numbers after it. The txt portion of the file is like:

...
x0 1 0 1 0.5 0
dx0 0 0 1 0 0


        
相关标签:
1条回答
  • 2021-01-29 13:30

    The program below shows how to read numbers from a line where the search string is the first word. It uses an internal read, which is often useful in Fortran I/O.

    program xinternal_read
    implicit none
    integer :: ierr
    integer, parameter :: iu = 20
    character (len=*), parameter :: search_str = "dx0"
    real :: xx(5)
    character (len=1000) :: text
    character (len=10) :: word
    open (unit=iu,file="foo.txt",action="read")
    do
       read (iu,"(a)",iostat=ierr) text ! read line into character variable
       if (ierr /= 0) exit
       read (text,*) word ! read first word of line
       if (word == search_str) then ! found search string at beginning of line
          read (text,*) word,xx
          print*,"xx =",xx
       end if
    end do
    end program xinternal_read
    

    The output is

    xx = 0. 0. 1. 0. 0.
    
    0 讨论(0)
提交回复
热议问题