" Fortran运行时错误:错误的实数" - 阅读文件

时间:2017-02-14 11:44:35

标签: fortran

我有以下外部文件,其中包含8行广告11列。无论如何都无法更改此文件。

Name        Sun         Jupiter         Saturn          Uranus          Neptune         EarthBC         Mercury         Venus           Mars            Pluto
mass(Msun)  1.000       9.547922048e-4  2.858857575e-4  4.366245355e-5  5.151391279e-5  3.040433607e-6  1.660477111e-7  2.448326284e-6  3.227778035e-7  6.607313077e-9              
a(AU)                   5.20219308      9.54531447      19.19247127     30.13430686     1.00000159      0.38709889      0.72332614      1.52364259      39.80634014
e                       0.04891224      0.05409072      0.04723911      0.00734566      0.01669714      0.20563613      0.00676922      0.09330305      0.25439724
I(deg)                  1.30376425      2.48750693      0.77193683      1.77045595      0.00090235      7.00457121      3.39460666      1.84908137      17.12113756
M(deg)                  240.35086842    045.76754755    171.41809349    293.26102612    094.81131358    242.19484206    345.30814403    330.93171908    024.68081529
w(deg)                  274.15634048    339.60245769    098.79773610    255.50375800    286.84104687    029.14401042    054.54948603    286.56509772    114.39445491
OMEGA(deg)              100.50994468    113.63306105    073.98592654    131.78208581    176.14784451    048.32221297    076.66204037    049.53656349    110.32482041

此文件由以下正确编译的程序读取

  program readtable

  implicit none
  integer :: i, j, num_col, num_row
  double precision, dimension (8,11) :: a
  character(14), dimension (8) :: par

  num_col = 4  
  num_row = 8

  open(100,file='SSL.dat',status='old')
  do j=1, num_row

        read(100,*) par(j), (a(i,j), i=1,num_col)

  end do

  print *, par
  print *, a(2,3)    !Jupiter's Mass

  end program 

当我以Fortran90运行此程序时,我收到以下消息:

At line 14 of file test.f (unit = 100, file='SSL.dat')
Fortran runtime error: Bad real number in item 2 of list input

我想我需要制作一个FORMAT()语句来帮助程序正确读取文件,但我似乎无法使格式正确。

1 个答案:

答案 0 :(得分:1)

正如agentp所说,列表指示在这里很好,你只需考虑前两行是不同的。我会这样做(在这里稍微猜测 - 我不是100%确信我明白你想要的):

ian-admin@agon ~/test $ cat r.f90
Program readtable

  Implicit None

  Integer, Parameter :: wp = Selected_real_kind( 13, 70 )

  Integer :: i, j, num_col, num_row
  Real( wp ) :: msun
  Real( wp ), Dimension (9,11) :: a
  Character(14), Dimension (8) :: par

  num_col = 9 
  num_row = 7

  Open( 100, file = 'SSL.dat', status = 'old' )
  Read( 100, * )
  j = 1
  Read( 100, * ) par(j), msun, (a(i,j), i=1,num_col)
  Do j = 2, num_row

     Read(100,*) par(j), (a(i,j), i=1,num_col)

  End Do

  Write( *, * ) par
  Write( *, * ) a(2,3)    !Jupiter's Mass

End Program readtable
ian-admin@agon ~/test $ gfortran -std=f2003 -Wall -Wextra -O -fcheck=all r.f90
ian-admin@agon ~/test $ ./a.out  
 mass(Msun)    a(AU)         e             I(deg)        M(deg)        w(deg)        OMEGA(deg)    
   5.4090720000000002E-002