且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Fortran在txt文件中查找字符串

更新时间:2022-11-13 23:39:35

下面的程序显示了如何从搜索字符串为第一个单词的行中读取数字.它使用内部读取,这在Fortran I/O中通常很有用.

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

输出为

xx = 0. 0. 1. 0. 0.