从fortran的数据文件中读取列 [英] Reading columns from data file in fortran

查看:1412
本文介绍了从fortran的数据文件中读取列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写了下面的代码块来读取外部数据文件:

  open(unit = 338,file ='bounnodes (i,j)
read(338,001) b $ b write(6,*)'BOUNDARY NODES',NODEBOUN(i,j)
ENDDO
ENDDO
2001
格式(32I5)


$ b

据我所知,它应该从 bounnodes.dat
但是,在读取期间,我得到一个错误文件结束,它打印出第一列。



我试着用相同的代码读取一个32 x 2的数组,它读取第一列的32个元素,但为下一列输出 0s



你能解释一下发生了什么吗?我的格式化是错误的吗?

解决方案

Fortran中的每个读取语句都前进到下一个记录。这意味着正常的文本文件中的新行。试试这个:

$ $ p $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ read(338,2001,advance ='no')NODEBOUN(i,j)
write(*,*)'BOUNDARY NODES',NODEBOUN(i,j)
ENDDO
read 338,*)
ENDDO

其中NQBOUN是行数,NUMBOUNNODES(I)是连续的列数。 (我有一切问题,什么是32x2与2x32)

你可以使它更短,使用隐含的做

  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

甚至

  DO I = 1,NQBOUN 
read (*,*)'BOUNDARY NODES',NODEBOUN(i,1:NUMBOUNNODES(I))
ENDDO

所有这些都使用Fortran 90功能。


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. However, I get an error end-of-file during read and it prints the first column.

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.

Can you please explain what is happening? Is my formatting wrong?

解决方案

Every read statement in Fortran advances to the next record. 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. (I have allway problems, what is 32x2 vs. 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的数据文件中读取列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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