“字符*10 :: a"之间的区别和“字符 :: a(10)" [英] Difference between "character*10 :: a" and "character :: a(10)"

查看:34
本文介绍了“字符*10 :: a"之间的区别和“字符 :: a(10)"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Trying to refresh my Fortran 90 knowledge for a project, I have run into some oddity when using internal files. Consider the example code:

! ---- 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 

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

which when compiled with gfortran crashes, writing to 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

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

When declared with the *length notation the character array can be used for internal write, but not when declared with the name(length) notation. Furthermore I noticed that the *length notation seems to be allowed only for character arrays, while it is forbidden with an error message like

Error: Old-style type declaration INTEGER*40 not supported at (1)

用于其他数据类型.

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

What is the difference between these notations and why does it affect use as internal files?

推荐答案

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

character*40 :: string is a character string of length 40

character :: string*40 是一样的

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

character(len=40) :: string is also a character string of length 40

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

character :: string(40) is an array of 40 character strings of length 1

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

character*40 :: string(40) is an array of 40 character strings of length 40

character :: string(40)*40 是一样的

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

character(len=40) :: string(40) is an array of 40 character strings of length 40

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

Your second internal writes fails, because it writes to the first string in the array string2. The first string string2(1) is just 1 character long and that is too short. For that reason you get the end of record error condition, the message is too long for the supplied string.

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

Internal writes treat array elements as separate records (similar to separate lines). One can utilize arrays of string in internal writes if one has more records (lines) to write into the array.

这篇关于“字符*10 :: a"之间的区别和“字符 :: a(10)"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆