读入可变长度整数矩阵

时间:2016-09-14 15:55:18

标签: fortran fortran90

我试图阅读文件

10 5 6 78 5 120 5 6 84 9 5 1
1 3 2 4 5 2 3 4 1 2 1 3
1 4 7 8 12 13

具有可变长度的行。

我尝试通过一次读入一个数字来首先计算列中元素的数量,但似乎每次读取调用都会将我移动到下一行。有没有一种简单的方法来计算Fortran中单个行中的元素数量?

1 个答案:

答案 0 :(得分:2)

检查这是否有帮助 -

program count_words_text 
implicit none 
integer, parameter   :: nlen=1000 
character (len=nlen) :: text 
integer              :: nwords, pos, i 
  text = "foo boo 1 2 goo" 
  pos = 1 
  nwords = 0 
  loop: do 
    i = verify(text(pos:), ' ')  !-- Find next non-blank. 
    if (i == 0) exit loop        !-- No word found. 
    nwords = nwords + 1          !-- Found something. 
    pos = pos + i - 1            !-- Move to start of the word. 
    i = scan(text(pos:), ' ')    !-- Find next blank. 
    if (i == 0) exit loop        !-- No blank found. 
    pos = pos + i - 1            !-- Move to the blank. 
  end do loop 
  print*,nwords ! gives 5 
end program count_words_text