Fortran运行时错误:文件结束,FORMAT输入

时间:2016-03-03 12:36:07

标签: fortran

freq.txt

>FREQ //74
 4.163186002791e+04  3.578830331359e+04  3.076496349687e+04  2.644671278966e+04  2.273458304119e+04 
 1.954349752908e+04  1.680032112209e+04  1.444218412726e+04  1.241504140604e+04  1.067243373686e+04 
 9.174423035938e+03  7.886677033340e+03  6.779682426302e+03  5.828068476394e+03  5.010025548360e+03 
 1.737988920100e+03  1.284332855871e+03  1.104060538508e+03  8.158747205330e+02  7.013564117662e+02 
 6.029121922103e+02  5.182858606802e+02  4.455379022877e+02  2.433020871700e+02  2.091515701348e+02 
 1.797945089525e+02  1.545580816278e+02  1.328639052196e+02  9.818329499070e+01  7.255514128762e+01 
 5.361653963401e+01  4.609078195788e+01  3.962135930423e+01  3.406000172766e+01  2.927925083995e+01 
 2.516953864546e+01  2.163667639887e+01  1.859969593339e+01  1.598899398582e+01  1.374473698894e+01 
 1.181548977143e+01  1.015703673713e+01  8.731368506527e+00  7.505810795983e+00  6.452275569743e+00 
 5.546617302183e+00  4.768079596776e+00  4.098819479081e+00  3.523498461194e+00  3.028931005477e+00 
 2.603782330822e+00  2.238308635635e+00  1.924133783786e+00  1.654057335509e+00  1.421889523591e+00 
 1.222309392724e+00  1.050742850800e+00  9.032578372386e-01  7.764742057598e-01  6.674862562538e-01 
 5.737961402745e-01  4.932566139141e-01  3.133421372728e-01  2.315524554696e-01  1.990511474577e-01 
 1.711118080085e-01  1.470941072881e-01  1.264475938317e-01  1.086990789815e-01  9.344179207682e-02 
 8.032605785014e-02  6.905128236880e-02  5.935906385039e-02  5.102727046220e-02 

我的代码

program lk

implicit none
integer :: i
real,dimension(74) :: fr

open(10,file='freq.txt')
read(10,*)

do i=1,74
read(10,*)fr(i)
end do

end program

t line 11 of file lk.f90 (unit = 10, file = 'freq.txt')
Fortran runtime error: End of file

问题是什么?我应该如何设置正确的格式?

1 个答案:

答案 0 :(得分:1)

好的,答案可以放大我先前的评论。

从文件读取的每个格式化read语句都从一行的开头开始。或者,如果您愿意,每个此类read语句都在行尾结束。这里代码试图从少于74行的文件中读取74行,即运行时对象。

一种解决方案是使用现代Fortran的功能来读取多个元素。在一个简单的例子中,例如在一个语句read(10,*) fr中读取整个数组就足够了。 *格式表示 list-directed 输入,所有现代编译器(据我所知)将完成所有必要的驴工作,以获得74个实数来填充{{1超出那个档案。