Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/fortran/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Io Fortran在txt文件中查找字符串_Io_Fortran - Fatal编程技术网

Io Fortran在txt文件中查找字符串

Io Fortran在txt文件中查找字符串,io,fortran,Io,Fortran,我想知道如何在txt文件中找到一个字符串,并读取后面的数字。 文件的txt部分如下所示: ... x0 1 0 1 0.5 0 dx0 0 0 1 0 0 下面是我想做的: character :: c real :: v1(1:5), v2(1:5) integer :: i ... open(10, file="input.txt", action="read") ... read(unit = 10, fmt=*) c do while(c/='x') read(unit = 10

我想知道如何在txt文件中找到一个字符串,并读取后面的数字。 文件的txt部分如下所示:

...
x0 1 0 1 0.5 0
dx0 0 0 1 0 0
下面是我想做的:

character :: c
real :: v1(1:5), v2(1:5)
integer :: i
...
open(10, file="input.txt", action="read")
...
read(unit = 10, fmt=*) c
do while(c/='x')
   read(unit = 10, fmt=*) c
end do
read(unit = 10, fmt=*) c
read(unit = 10, fmt=*) v1(1), v1(2), v1(3), v1(4), v1(5)
read(unit = 10, fmt=*) c c
read(unit = 10, fmt=*) v2(1), v2(2), v2(3), v2(4), v2(5)
close(10)
是否有函数返回do while循环找到的位置?找到这个职位的更好方法是什么


谢谢

下面的程序演示了如何从搜索字符串为第一个单词的行中读取数字。它使用内部读取,这在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.

到目前为止,你尝试了什么?我是fortran新手,我不知道如何在txt文件中查找字符串。我们编写你知道如何编写的代码,并清楚地指出(希望如此)仍然存在的小困难。因此,这并不是一个学习Fortran(或任何其他编程语言)编程基础知识的地方,我们中很少有人愿意为您编写所有代码。我查找了index函数,但它在字符串中查找子字符串。但是,我必须在txt文件中查找它。一种方法是将我所有的txt文件添加到一个字符串中,但这似乎不合理。或者我可以使用index函数在txt中查找字符串吗?