这与格式的处理方式有关,特别是与数据传输语句何时终止有关。
对于像您这样的输出语句,传输在以下任一情况下终止:
- 到达数据编辑描述符并且输出列表中没有剩余元素;或者
- 已到达最后的右括号,并且输出列表中没有剩余元素。
以您的格式
'(1x,"NN_DM:",1x,*("NN_",i2.2,1x))'
and
'(1x,"NN_DM:",1x,5("NN_",i2.2,1x))'
单个数据编辑描述符是i2.2
. The 1x
s 是控制编辑描述符,"NN_DM"
and "NN_"
是字符串编辑描述符。
让我们看看在这种情况下您的格式是如何处理的5
作为重复计数。第一部分格式1x,"NN_DM:",1x
处理后不会出现输出问题NN_DM:
让我们继续5("NN_",i2.2,1x))
。对应于这个重复片段的是五个数据项,因此它们被处理(给出输出NN_01 NN_02 NN_03 NN_04 NN_5
).
重要的是接下来会发生什么。完成此操作后5(..)
我们到达格式规范的最后一个右括号并且没有剩余的输出项,至此格式处理结束。
与有什么不同*(..)
case?
Well, when we reach the end of *(..)
we go back round to the start of that repeated format; we don't move on to the final closing parenthesis.1 That leaves us to process the edit descriptors until we reach a data edit descriptor. This means that "NN_"
is processed (resulting in NN_
being output) before we notice that we are out of data items for output.
解决办法:使用冒号编辑描述符。冒号编辑描述符的作用类似于数据编辑描述符,如果没有剩余数据项,格式处理将立即终止。
Character(56),parameter:: FMT_01 = '(1x,"NN_DM:",1x,*("NN_",i2.2,:,1x))'
就我个人而言,我会把它写成
Character(*),parameter:: FMT_01 = '(" NN_DM:",*(" NN_",i2.2,:))'
1 This would be no different if we had 6
as the repeat count; *
isn't special except that it is a "very large repeat count".