格式化读取和隐含执行 [英] Formatted reads and implied do

查看:7
本文介绍了格式化读取和隐含执行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 Fortran 中,可以选择使用隐含循环.它们通常用于打印,具有以下结构.

In Fortran one has the option to use implied loops. They are usually used for printing and have the following structure.

write(*,'(5I6)') (i,i=1,20)

! output 
 1     2     3     4     5
 6     7     8     9    10
11    12    13    14    15
16    17    18    19    20

这将允许写入 5 个宽度为 6 的整数,完成后自动转到下一行.因此,它对于格式化非常有用.

This would allow to write 5 integers of width 6 and when done, go automatically to the next line. Therefore, it is quite useful for formatting.

问题是这是否可以反过来进行

The question is if this can be done in reverse

read(*,'(5I6)') (a(i),i=1,20)

这当然行不通,因为它是写的,因为每行只有 5 个元素.无论如何,我想问一下如何在自动模式下翻转到下一行.我目前正在计算条目数,一旦达到 5,我就转到下一行.另一种选择是读取二进制暂存文件中的文件,其中不存在新"行.这对我来说不太适用,因为我使用 index() 函数进行关键字搜索.

This would of course not work, as it is written, because there are only 5 elements per line. Never the less I would like to ask how to flip to the next line in an automatic mode. I am currently counting the number of entries and once 5 is reached, I go to the next line. Another option would be to read the file in a binary scratch file, where no 'new' lines exist. This would not work neatly for me, because I use keywords search with the index() function.

推荐答案

如果你出于某种原因真的需要格式,我会这样做

I would do it like this if you really need the format for some reason

do i = 1, n, 5
  read(*,'(5I6)') a(i:i+4)
end do

否则,按照高性能标记的建议,列表导向格式会很好地工作.

otherwise the list directed format would work well as suggested by High Performance Mark.

你可以把它写成一个隐含的做,但子数组更好.

You could write it as an implied do, but sub-arrays are better.

我什至可以想象一个包含 / 的格式描述符的单行,但是为什么要麻烦呢?

I could even imagine a one liner with a format descriptor including /, but why bother?

(评论太长)

这篇关于格式化读取和隐含执行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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