Fortran Coder

关于二维数组读入,这个错误原因是什么?

查看数: 10936 | 评论数: 5 | 收藏 0
关灯 | 提示:支持键盘翻页<-左 右->
    组图打开中,请稍候......
发布时间: 2015-3-13 17:31

正文摘要:

[Fortran] 纯文本查看 复制代码Program t   Implicit None   Integer, Parameter :: row = 3   Integer, Parameter :: col = 4   Real :: a(1:row,1:col)    &nb ...

回复

likm1110 发表于 2015-3-13 21:55:59
fcode 发表于 2015-3-13 18:47
因为你执行了 12 次循环,所以需要12行。

如无特别声明,每个 read 读一行。如果你只有 3 行,可以这样读 ...

我好好研究一下。感谢!
fcode 发表于 2015-3-13 18:47:54
因为你执行了 12 次循环,所以需要12行。

如无特别声明,每个 read 读一行。如果你只有 3 行,可以这样读:

[Fortran] 纯文本查看 复制代码
Program t
  Implicit None
  Integer, Parameter :: row = 3
  Integer, Parameter :: col = 4
  Real :: a(1:row,1:col)   
  Integer :: i,j  
  Open(11,file='in.txt')
   Do i = 1,row
    Do j = 1,col
      a(i,j) = 10*i+j
      Write(11,*) a(i,j)
    Enddo
  Enddo
  ! read the array==============================================
  Do i = 1,row
    Do j = 1,col
      Read(11,*) a(i,1)
    Enddo
  Enddo
  Close(11)
End Program t


更多的二维数组与文本文件行列问题,请阅读:
http://fcode.cn/guide-45-1.html
fcode 发表于 2015-3-13 18:04:00
[Fortran] 纯文本查看 复制代码
Program t
  Implicit None
  Integer, Parameter :: row = 3
  Integer, Parameter :: col = 4
  Real :: a(1:row,1:col)
  Integer :: i,j
  Open(11,file='in.txt')
  Do i = 1,row
      Read(11,*) a(i,:)
      Write(*,*) a(i,:)
  Enddo
  Close(11)
End Program t


当你对 11 号文件写入后,文件操作位置就在文件的最末端,此时你再读取,后面没有数据,所以会出错。

你可以把文件操作位置重新设置为文件开始。用 Rewind 语句。

也可以关闭文件,然后重新读取。

(一般情况下,同一次 Open Close,不要即读又写,比较容易出错)
li913 发表于 2015-3-13 18:00:29
end-of-file 错误:
http://fcode.cn/guide-36-2.html

捐赠本站|Archiver|关于我们 About Us|小黑屋|Fcode ( 京ICP备18005632-2号 )

GMT+8, 2025-4-20 03:27

Powered by Tencent X3.4

© 2013-2025 Tencent

快速回复 返回顶部 返回列表