格式化读取和暗示做 [英] Formatted reads and implied do
问题描述
write(*,'(5I6)')(i,i = 1,20)
!输出
1 2 3 4 5
6 7 8 9 10
11 12 13 14 15
16 17 18 19 20
这将允许写入5个宽度为6的整数,完成后自动进入下一行。因此,格式化非常有用。
问题是,如果这可以在相反的情况下完成
read (*,'(5I6)')(a(i),i = 1,20)
这当然不起作用,因为每行只有5个元素。从来就不是我想问如何在自动模式下翻到下一行。我目前正在统计入场次数,并且一旦达到5次,我就去下一行。另一种选择是在不存在新行的二进制暂存文件中读取文件。这对我来说不太合适,因为我用 index()
函数使用关键字搜索。
我会这样做,如果你真的需要格式的原因
do i = 1,n,5
read(*,'(5I6)')a(i:i + 4)
end do
否则,按照高性能标记的建议,列表定向格式将运行良好。
您可以将其编写为隐含的,但是子数组更好。
我甚至可以想象一个带有格式描述符的单线程,包括 /
,但为什么要麻烦?
(太长的评论)
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
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)
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?
(too long for a comment)
这篇关于格式化读取和暗示做的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!