如何使用fortran读取不规则行 [英] How to use fortran to read irregular lines
本文介绍了如何使用fortran读取不规则行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个格式如下的文本文件:
I have a text file formatted like this:
1 2
3 4 5
6 7
以此类推,适用于许多行.
and so on for many lines.
我运行此fortran程序:
I run this fortran program:
i=1
tt=1
do while(.true.)
read(unit=1,*,IOSTAT=status) lon(i,tt),lat(i,tt),h(i,tt)
i=i+1
if(status/=0)exit
enddo
我想在同一行中将三个数字分配给lon
,lat
,h
.但是,由于第一行没有第三个元素,所以程序将读取第二行中的第一个元素(即3到h(i,tt)),这不是我想要的.我想要将h(i,tt)设置为某些行中的缺失值
I want to assign three numbers in the same line to lon
, lat
, h
. However, because the first line doesn't have the third element , the program will read the first element in the second line (i.e., 3 to h(i,tt)), and that's not what I want. I want to set h(i,tt) to the missing value in some lines
我该怎么做?
推荐答案
对于您的特定示例,您可以尝试执行以下操作:
For your specific example, you can try something like this:
program test
integer :: status, i, tt
character(len=100) :: line
integer :: lon(3, 1), lat(3, 1), h(3, 1)
lon(:, :) = 0
lat(:, :) = 0
h(:, :) = 0
open(unit=1, file='data.txt')
i=1
tt=1
do
read(1, '(A100)', iostat=status) line
if(status/=0) exit
read(line, *, iostat=status) lon(i, tt), lat(i, tt), h(i, tt)
if(status/=0) then
read(line, *) lon(i, tt), lat(i, tt)
h(i, tt) = 1 ! default value
end if
i = i + 1
end do
print *, 'lon=', lon
print *, 'lat=', lat
print *, 'h=', h
end program test
返回
lon= 1 3 6
lat= 2 4 7
h= 1 5 1
这篇关于如何使用fortran读取不规则行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文