将FORTRAN READ参数传递给字符串 [英] pass FORTRAN READ arguments into a string
问题描述
我有一个包含要读取的变量名称的字符串,我想将此字符串传递给read函数.这可以让我更改读取的变量名,而只需更改带有变量名的向量即可. 一个例子可能是:
I have a string including the names of the variables I want read, and I would like to pass this string to the read function. This could allow me to change the name of the variables I read just changing the vector with the names of the variables. An example could be:
PROGRAM test
implicit none
integer :: no, age
character(len=20) :: myname, vars
vars='no, myname, age'
read(*, '(i4,a20,i4)') vars
print*, no, myname, age
END PROGRAM test
这可能吗?
推荐答案
您可以查看"NAMELIST" I/O,这也许是您要执行的操作.通常,名称列表IO会遇到各种问题,并且人们经常反而会编写自己的自定义IO例程.但是,如果满足您的需求就足够了,那么它就很容易使用.例如
You can look into "NAMELIST" I/O, which maybe does what you're after. Often, namelist IO has various issues, and people often resort to writing their own custom IO routines anyway. But if it's enough for what you want, it's quite easy to use. E.g.
program nmltest
implicit none
real :: x
integer :: y
namelist /mynml/ x, y
x = 4711
y = 42
write(*, mynml)
end program nmltest
这篇关于将FORTRAN READ参数传递给字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!