将包含(大)N 个实数的行读取到 Fortran 数组中
我已经通过 read() 读取了输入文件的标头,并在途中读取了 L 的值。
现在我遇到一行 L^2 连续实数,我需要将其输入到可分配数组 A(L,L) 的元素中。
尝试
character *100 :: buffer
read (1,10) buffer
10 format(a(L*10))
结果,
Error: Syntax error in FORMAT statement at (1)
Error: FORMAT label 10 at (1) not defined
但我不确定如何处理(巨大的)可变数量的实数。
Trying:
do i=1,L
do j=i,L
read (1,"(f10.7)") buffer
read (buffer,*) A(i,j)
enddo
enddo
throws:
Fortran runtime error: Expected REAL for item 2 in formatted transfer, got CHARACTER
(f10.7)
我不能简单地读取(1,"(a1000)"),因为 L 最终会变得很大,所以我真正需要的是一种逐个解析元素的方法。
请问有办法吗?
I've read() down past a header of an input file, and read the value of L on the way.
Now I come to a line of L^2 consecutive reals, which I need to input to the elements of an allocatable array A(L,L).
Trying
character *100 :: buffer
read (1,10) buffer
10 format(a(L*10))
results in
Error: Syntax error in FORMAT statement at (1)
Error: FORMAT label 10 at (1) not defined
but I'm not sure how else to deal with a (hugely) variable number of reals.
Trying:
do i=1,L
do j=i,L
read (1,"(f10.7)") buffer
read (buffer,*) A(i,j)
enddo
enddo
throws:
Fortran runtime error: Expected REAL for item 2 in formatted transfer, got CHARACTER
(f10.7)
I can't simply read(1,"(a1000)") as L will eventually end up huge, so what I really need is a way to parse the elements one by one.
Please say there's a way?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
处理完标头后,也许可以通过将其读入字符串并解析该字符串来执行“奇特”的操作,为什么不直接从文件中读取数字并跳过字符“缓冲区”呢?
“read (unit, *) A”被称为“list-directed IO”——如果你想知道要搜索或查找什么——这对我来说似乎是一个很好的方法。它非常灵活——您不必担心将数字精确地对齐到列中。如果您只是读入数组“A”,则将按 Fortran 数组元素顺序读取元素。
在 Fortran 2003 中,您可以使用“*”作为变量格式重复说明符:read (unit, '( *(F10.7) )' )。然而,目前还没有多少编译器支持这一点。最简单的事情就是使用一个巨大的值,大于您所需要的值 - 当列表上没有更多项目要读取时,读取将停止 - 重复说明符允许超过项目数读。
After you have processed the header, perhaps by doing "fancy" things by reading it into a string and parsing the string, why not just directly read the numbers from the file and skip the character "buffer"?
"read (unit, *) A" is called "list-directed IO" -- if you want to know what to search for or look up -- it seems like a good approach to me. It is very flexible -- you don't have to be concerned with precisely aligning you numbers into columns. If you just read into the array "A", the elements will be read in Fortran array element order.
In Fortran 2003, you can use "*" as a variable format repeat specifier: read (unit, '( *(F10.7) )' ). However, not many compilers support this yet. This easiest thing to do is just to use a huge value, larger than you will ever need -- the read will stop when there are no more items on the list to be read -- the repeat specifier is allowed to exceed the number of items read.
这是您要找的吗?
http://www.tek-tips.com/viewthread。 cfm?qid=1420862&page=1
更新:
http://www.cs.mtu.edu/~shene/COURSES/cs201/NOTES/chap05/format.html
http://rainbow.ldgo.columbia.edu/data/fortranreaddata.html
这涉及未格式化的记录长度读取。我已经很长时间没有搞过 Fortran I/O 了。我非常确定有一个 OPEN 或 READ 标志,指定它不应继续到下一行,而是将文件指针保留在适当的位置,以便下一次 READ 可以从那里开始。但我一时想不起来...
Is this what you were looking for?
http://www.tek-tips.com/viewthread.cfm?qid=1420862&page=1
Update:
http://www.cs.mtu.edu/~shene/COURSES/cs201/NOTES/chap05/format.html
http://rainbow.ldgo.columbia.edu/data/fortranreaddata.html
This goes over unformatted record-length reading. It's been a long time since I've had to mess with fortran I/O. I'm pretty certain there's a flag to either OPEN or READ that specifies that it shouldn't continue onto the next line, but rather keep the file pointer in place so that the next READ can start from there. But I can't remember it off-hand...
哈哈,这似乎有效:
read (1,*) A
write (*,*) A
似乎标准知道它在做什么,即使我不知道。
Haha, this seems to work:
read (1,*) A
write (*,*) A
Seems the standard knows what it's doing, even if I don't.