[英]Reading columns from data file in fortran
I wrote the following block to read from an external data file: 我编写了以下块以读取外部数据文件:
open(unit=338,file='bounnodes.dat',form='formatted')
DO I=1,NQBOUN
DO J=1,NUMBOUNNODES(I)
read(338,2001) NODEBOUN(i,j)
write(6,*) 'BOUNDARY NODES', NODEBOUN(i,j)
ENDDO
ENDDO
2001
FORMAT(32I5)
As far as I understood, this should read a 2 x 32 array from bounnodes.dat
. 据我了解,这应该从
bounnodes.dat
读取2 x 32数组。 However, I get an error end-of-file
during read and it prints the first column. 但是,在读取过程中出现错误
end-of-file
并且它打印第一列。
I tried to read a 32 x 2 array using the same code, and it reads 32 elements of the first column, but outputs 0s
for the next column. 我试图使用相同的代码读取32 x 2的数组,它读取第一列的32个元素,但为下一列输出
0s
。
Can you please explain what is happening? 您能解释一下发生了什么吗? Is my formatting wrong?
我的格式有误吗?
Every read statement in Fortran advances to the next record. Fortran中的每个已读语句都会前进到下一条记录。 This means a new line in normal text files.
这意味着普通文本文件中将换行。 Try this:
尝试这个:
DO I=1,NQBOUN
DO J=1,NUMBOUNNODES(I)
read(338,2001,advance='no') NODEBOUN(i,j)
write(*,*) 'BOUNDARY NODES', NODEBOUN(i,j)
ENDDO
read(338,*)
ENDDO
where NQBOUN is number of rows and NUMBOUNNODES(I) is number of columns in a row. 其中NQBOUN是行数,NUMBOUNNODES(I)是一行中的列数。 (I have allway problems, what is 32x2 vs. 2x32)
(我总是遇到问题,什么是32x2与2x32)
You can make it even shorter, using the implied do 您可以使用隐含的方法使其更短
DO I=1,NQBOUN
read(338,2001) ( NODEBOUN(i,j) , j=1,NUMBOUNNODES(I) )
write(*,*) ( 'BOUNDARY NODES', NODEBOUN(i,j) , j=1,NUMBOUNNODES(I) )
ENDDO
or even 甚至
DO I=1,NQBOUN
read(338,2001) NODEBOUN(i,:)
write(*,*) 'BOUNDARY NODES', NODEBOUN(i,1:NUMBOUNNODES(I))
ENDDO
All of these use Fortran 90 features. 所有这些都使用Fortran 90功能。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.