以下脚本创建派生类型,然后尝试将其名称列表输出到文本文件:
program test
implicit none
character(len=:), allocatable :: CurrentString
integer :: linelength, IO
type SubjectType
character(20) :: genre
character(20) :: maindude
end type SubjectType
type BookType
character(20) :: title
character(20) :: author
type(SubjectType) :: subject
integer, dimension(2,2):: array
integer :: BookID
end type Booktype
type(Booktype) :: Book
Book%title = "Harry Potter"
Book%author = "JK Rowling"
Book%subject%genre = "Fantasy"
Book%subject%maindude = "Ron Weasley"
Book%array = RESHAPE([1,2,3,4],[2,2])
Book%BookID = 105
open(10, file = 'namelist.txt')
namelist /mynamelist/ Book
write(10, nml = mynamelist)
close(10, status = 'keep')
end program test
编译时会抛出错误:
|| === Build:Hello in Hello(编译器:GNU Fortran编译器)=== |
D:\ TEMP \ Hello \ main.f95 | 30 |错误:意外的NAMELIST语句|
D:\ TEMP \ Hello \ main.f95 | 31 |错误:符号' mynamelist'必须是
NAMELIST组名| || ===构建失败:2个错误,0个警告(0
分钟,2秒(秒)=== |
google search确实不是很有帮助。我找到的唯一论坛会讨论为什么会发生这种情况,而不是如何修复它。我怎样才能在GFortran中运行它?请注意,这在IFort中有效。
答案 0 :(得分:1)
NAMELIST是一份声明声明。它不能放在可执行语句之间。它只能在第一个可执行语句之前的每个单元的开头使用。
type(Booktype) :: Book
namelist /mynamelist/ Book
Book%title = "Harry Potter"
...
open(10, file = 'namelist.txt')
write(10, nml = mynamelist)
close(10, status = 'keep')