"character*10 :: a"和"character :: a(10)"之间的区别

kdb*_*kdb 5 arrays syntax fortran

试图为项目刷新我的Fortran 90知识,在使用内部文件时遇到了一些奇怪的问题.考虑示例代码:

! ---- internal_file_confusion.f90 ----
program internal_file_confusion
  implicit none 

  character*40 :: string1
  character :: string2(40)

  write(string1, *) "Hello World 1"
  write(*,*) "string1 = ", string1

  write(string2, *) "Hello World 2"
  write(*,*) "string2 = ", string2

end program 
Run Code Online (Sandbox Code Playgroud)

当用gfortran崩溃编译时,写入STDOUT

 string1 =  Hello World 1                          
At line 10 of file e:/Daten/tmp/fortran-training/internal_file_confusion.f90
Fortran runtime error: End of record
Run Code Online (Sandbox Code Playgroud)

使用*length表示法声明时,字符数组可用于内部写入,但在使用name(length)表示法声明时则不能.此外,我注意到*length符号似乎只允许用于字符数组,而禁止使用类似的错误消息

Error: Old-style type declaration INTEGER*40 not supported at (1)
Run Code Online (Sandbox Code Playgroud)

对于其他数据类型.

这些符号之间有什么区别?为什么它会影响内部文件的使用?

Vla*_*r F 9

character*40 :: string 是一个长度为40的字符串

character(len=40) :: string 也是一个长度为40的字符串

character :: string(40) 是一个长度为1的40个字符串的数组

character*40 :: string(40) 是一个由40个长度为40的字符串组成的数组

character(len=40) :: string(40) 是一个由40个长度为40的字符串组成的数组

您的第二次内部写入失败,因为它写入数组中的第一个字符串string2.第一个字符串string2(1)只有1个字符,而且太短.因此,您得到记录错误条件的结束,消息对于提供的字符串来说太长了.

内部写入将数组元素视为单独的记录(类似于单独的行).如果有更多记录(行)写入数组,则可以在内部写入中使用字符串数组.