如何在 Fortran 90 中跳过带有“#”符号的行? [复制]
Posted
技术标签:
【中文标题】如何在 Fortran 90 中跳过带有“#”符号的行? [复制]【英文标题】:How to skip lines with "#" symbol in Fortran 90? [duplicate] 【发布时间】:2014-07-01 02:06:57 【问题描述】:我正在尝试从 Fortran 90 中的 .dat 文件中读取一个数字。问题在于,这个 .dat 文件中的行以“#”符号开头,我希望 Fortran 忽略这些行。这是我正在阅读的文件
#number of free parameters= 252
#used norm for the fit= 3.231250e+02
#0 0 0 0 0 -6.458147e+06
#0 0 0 0 1 3.105635e+01
#0 0 0 0 2 2.525518e-04
#0 0 0 0 3 -4.281012e-09
#0 0 0 0 4 -2.759312e-14
#0 0 0 0 5 1.534108e-19
#0 0 0 1 0 6.646356e-01
#0 0 0 1 1 7.311006e-04
#0 0 0 1 2 -2.619385e-09
#0 0 0 1 3 -2.686922e-14
#0 0 0 1 4 -1.679757e-20
#0 0 0 2 0 -3.188727e-04
#0 0 0 2 1 -6.512261e-10
#0 0 0 2 2 2.366201e-14
#0 0 0 2 3 -7.321188e-20
#0 0 0 3 0 -2.747463e-09
#0 0 0 3 1 -2.713283e-15
#0 0 0 3 2 6.595914e-20
#0 0 0 4 0 -3.916931e-14
#0 0 0 4 1 -1.166710e-19
#0 0 0 5 0 7.337420e-20
#0 0 1 0 0 1.098464e+02
#0 0 1 0 1 4.974315e-04
#0 0 1 0 2 2.851914e-09
#0 0 1 0 3 -2.829991e-14
#0 0 1 0 4 -1.078132e-19
#0 0 1 1 0 -3.428791e-04
#0 0 1 1 1 2.540000e-09
#0 0 1 1 2 -5.170717e-14
#0 0 1 1 3 9.467328e-20
#0 0 1 2 0 1.536840e-09
#0 0 1 2 1 -2.862305e-14
#0 0 1 2 2 2.214778e-19
#0 0 1 3 0 -7.982451e-15
#0 0 1 3 1 -1.115507e-19
#0 0 1 4 0 -1.784763e-19
#0 0 2 0 0 -3.354193e-04
#0 0 2 0 1 -2.608933e-09
#0 0 2 0 2 1.603134e-14
#0 0 2 0 3 1.544635e-21
#0 0 2 1 0 9.286391e-11
#0 0 2 1 1 2.212860e-14
#0 0 2 1 2 4.500369e-19
#0 0 2 2 0 4.355379e-14
#0 0 2 2 1 -1.988663e-19
#0 0 2 3 0 1.375598e-19
#0 0 3 0 0 -5.089157e-09
#0 0 3 0 1 -6.290206e-15
#0 0 3 0 2 6.371429e-20
#0 0 3 1 0 -1.164189e-14
#0 0 3 1 1 1.980810e-19
#0 0 3 2 0 -1.530186e-20
#0 0 4 0 0 -8.497320e-15
#0 0 4 0 1 -3.419311e-20
#0 0 4 1 0 1.151535e-19
#0 0 5 0 0 -4.965320e-20
#0 1 0 0 0 -1.259427e+02
#0 1 0 0 1 -3.900548e-05
#0 1 0 0 2 3.691997e-09
#0 1 0 0 3 1.526078e-14
#0 1 0 0 4 2.153889e-19
#0 1 0 1 0 -1.518887e-04
#0 1 0 1 1 1.725796e-09
#0 1 0 1 2 -1.001640e-14
#0 1 0 1 3 1.515356e-19
#0 1 0 2 0 2.629831e-09
#0 1 0 2 1 6.313110e-15
#0 1 0 2 2 3.219031e-19
#0 1 0 3 0 2.906599e-14
#0 1 0 3 1 2.167608e-19
#0 1 0 4 0 1.696993e-19
#0 1 1 0 0 -9.267004e-04
#0 1 1 0 1 2.093122e-09
#0 1 1 0 2 4.148898e-14
#0 1 1 0 3 9.015903e-20
#0 1 1 1 0 -5.283470e-09
#0 1 1 1 1 2.942465e-14
#0 1 1 1 2 -1.620549e-19
#0 1 1 2 0 -1.392080e-15
#0 1 1 2 1 4.239008e-21
#0 1 1 3 0 4.896848e-20
#0 1 2 0 0 2.685715e-09
#0 1 2 0 1 1.666833e-14
#0 1 2 0 2 -7.874688e-20
#0 1 2 1 0 9.224232e-15
#0 1 2 1 1 -4.773437e-19
#0 1 2 2 0 -1.519710e-19
#0 1 3 0 0 5.053813e-14
#0 1 3 0 1 4.160181e-20
#0 1 3 1 0 1.370084e-19
#0 1 4 0 0 -8.886168e-21
#0 2 0 0 0 -2.300488e-04
#0 2 0 0 1 7.564571e-09
#0 2 0 0 2 -1.102233e-14
#0 2 0 0 3 -6.403138e-21
#0 2 0 1 0 -4.206376e-09
#0 2 0 1 1 -3.403031e-14
#0 2 0 1 2 -3.346447e-19
#0 2 0 2 0 5.381533e-15
#0 2 0 2 1 -1.823920e-19
#0 2 0 3 0 -1.178995e-19
#0 2 1 0 0 -1.655777e-09
#0 2 1 0 1 2.935844e-14
#0 2 1 0 2 -6.278178e-20
#0 2 1 1 0 1.300195e-14
#0 2 1 1 1 -8.475176e-21
#0 2 1 2 0 1.052778e-19
#0 2 2 0 0 3.096973e-15
#0 2 2 0 1 1.549711e-19
#0 2 2 1 0 -7.566176e-20
#0 2 3 0 0 -2.251950e-19
#0 3 0 0 0 -3.739465e-09
#0 3 0 0 1 2.295125e-14
#0 3 0 0 2 -4.084332e-20
#0 3 0 1 0 4.950778e-15
#0 3 0 1 1 -1.106986e-19
#0 3 0 2 0 -4.027040e-20
#0 3 1 0 0 1.621460e-14
#0 3 1 0 1 -1.062879e-19
#0 3 1 1 0 -2.483709e-19
#0 3 2 0 0 1.690545e-19
#0 4 0 0 0 6.163400e-15
#0 4 0 0 1 6.288739e-20
#0 4 0 1 0 1.192787e-19
#0 4 1 0 0 -1.455520e-19
#0 5 0 0 0 -9.933712e-20
#1 0 0 0 0 -3.666264e+01
#1 0 0 0 1 9.549133e-04
#1 0 0 0 2 -4.365234e-09
#1 0 0 0 3 2.588782e-16
#1 0 0 0 4 -2.076915e-20
#1 0 0 1 0 1.481452e-03
#1 0 0 1 1 1.904617e-09
#1 0 0 1 2 -1.930502e-14
#1 0 0 1 3 2.011388e-20
#1 0 0 2 0 8.142289e-11
#1 0 0 2 1 -2.718983e-14
#1 0 0 2 2 -8.626389e-20
#1 0 0 3 0 1.100715e-14
#1 0 0 3 1 -6.753546e-21
#1 0 0 4 0 1.454479e-19
#1 0 1 0 0 1.163360e-03
#1 0 1 0 1 -1.811381e-09
#1 0 1 0 2 -3.732141e-14
#1 0 1 0 3 -1.731446e-19
#1 0 1 1 0 -1.368255e-09
#1 0 1 1 1 -1.199297e-14
#1 0 1 1 2 -1.829752e-19
#1 0 1 2 0 8.200737e-15
#1 0 1 2 1 -1.118786e-19
#1 0 1 3 0 -9.719783e-20
#1 0 2 0 0 -2.880947e-09
#1 0 2 0 1 1.251979e-14
#1 0 2 0 2 -1.047222e-19
#1 0 2 1 0 -1.228216e-14
#1 0 2 1 1 9.662320e-20
#1 0 2 2 0 -3.024362e-21
#1 0 3 0 0 -2.423240e-14
#1 0 3 0 1 8.306093e-20
#1 0 3 1 0 -1.033559e-19
#1 0 4 0 0 -7.429864e-20
#1 1 0 0 0 6.989398e-04
#1 1 0 0 1 -1.581314e-09
#1 1 0 0 2 -2.193096e-14
#1 1 0 0 3 -1.899676e-19
#1 1 0 1 0 3.866920e-09
#1 1 0 1 1 -5.727360e-15
#1 1 0 1 2 -3.347686e-20
#1 1 0 2 0 -2.266355e-14
#1 1 0 2 1 3.559480e-19
#1 1 0 3 0 3.783552e-20
#1 1 1 0 0 -4.351640e-09
#1 1 1 0 1 -4.493149e-14
#1 1 1 0 2 3.886234e-20
#1 1 1 1 0 1.795029e-14
#1 1 1 1 1 -1.723292e-19
#1 1 1 2 0 3.898557e-19
#1 1 2 0 0 3.297662e-14
#1 1 2 0 1 -6.122045e-20
#1 1 2 1 0 -3.220305e-20
#1 1 3 0 0 1.550060e-19
#1 2 0 0 0 1.132462e-09
#1 2 0 0 1 9.649800e-15
#1 2 0 0 2 -1.487728e-19
#1 2 0 1 0 -1.033199e-14
#1 2 0 1 1 -1.066775e-19
#1 2 0 2 0 -1.074791e-19
#1 2 1 0 0 -5.672266e-14
#1 2 1 0 1 -4.539318e-19
#1 2 1 1 0 4.278144e-20
#1 2 2 0 0 -1.761885e-20
#1 3 0 0 0 3.350312e-14
#1 3 0 0 1 1.410364e-19
#1 3 0 1 0 1.155801e-19
#1 3 1 0 0 1.753253e-19
#1 4 0 0 0 2.302075e-19
#2 0 0 0 0 -1.251751e-03
#2 0 0 0 1 -1.528697e-09
#2 0 0 0 2 2.022063e-14
#2 0 0 0 3 3.612229e-19
#2 0 0 1 0 -1.394598e-09
#2 0 0 1 1 1.493080e-14
#2 0 0 1 2 2.747549e-19
#2 0 0 2 0 1.440434e-14
#2 0 0 2 1 -1.147694e-19
#2 0 0 3 0 5.537588e-21
#2 0 1 0 0 6.623155e-09
#2 0 1 0 1 2.730209e-15
#2 0 1 0 2 4.181196e-20
#2 0 1 1 0 1.488522e-14
#2 0 1 1 1 -1.643706e-20
#2 0 1 2 0 -3.837800e-19
#2 0 2 0 0 -3.120561e-14
#2 0 2 0 1 9.825753e-20
#2 0 2 1 0 1.769783e-19
#2 0 3 0 0 -1.549952e-19
#2 1 0 0 0 -3.557290e-09
#2 1 0 0 1 1.033207e-14
#2 1 0 0 2 -1.516672e-19
#2 1 0 1 0 1.499897e-14
#2 1 0 1 1 1.675754e-20
#2 1 0 2 0 1.290456e-20
#2 1 1 0 0 3.365246e-14
#2 1 1 0 1 1.912043e-19
#2 1 1 1 0 1.251717e-19
#2 1 2 0 0 4.824158e-20
#2 2 0 0 0 -6.775576e-15
#2 2 0 0 1 3.018091e-19
#2 2 0 1 0 -1.955331e-19
#2 2 1 0 0 1.276077e-20
#2 3 0 0 0 2.575621e-20
#3 0 0 0 0 -8.039580e-10
#3 0 0 0 1 -6.643968e-14
#3 0 0 0 2 1.627232e-19
#3 0 0 1 0 -8.617469e-15
#3 0 0 1 1 1.842556e-19
#3 0 0 2 0 -6.082756e-20
#3 0 1 0 0 -2.047656e-15
#3 0 1 0 1 -6.400721e-20
#3 0 1 1 0 -1.705284e-19
#3 0 2 0 0 3.026086e-19
#3 1 0 0 0 4.164057e-14
#3 1 0 0 1 -6.256086e-20
#3 1 0 1 0 -9.961920e-20
#3 1 1 0 0 -2.004688e-19
#3 2 0 0 0 -4.014110e-19
#4 0 0 0 0 1.059104e-14
#4 0 0 0 1 -3.637782e-20
#4 0 0 1 0 -2.760611e-21
#4 0 1 0 0 1.484074e-19
#4 1 0 0 0 4.916317e-20
#5 0 0 0 0 -1.274646e-20
#average insample error= 1.010769e-01
1.374988e+05
我只需要读取值“1.374988e+05”,知道吗?
【问题讨论】:
跳过空读取的行或使用iostat
并使用第一个成功读取。或者将该行作为文本阅读并检查#
。在这里讨论过很多次。
可能重复 ***.com/questions/5611677/… ***.com/questions/10259712/…
非常感谢,我使用 IOstat 条件解决了。
【参考方案1】:
该程序通过执行“跳过空读取行或使用 iostat 并使用第一次成功读取”的建议来解决问题。
program xread_data
implicit none
integer, parameter :: iu = 10
real :: x
integer :: ierr
open (unit=iu,file="data.txt",action="read")
do
read (iu,*,iostat=ierr) x
if (ierr == 0) exit
end do
print*,"x =",x
end program xread_data
【讨论】:
以上是关于如何在 Fortran 90 中跳过带有“#”符号的行? [复制]的主要内容,如果未能解决你的问题,请参考以下文章