如何从文本文件中读取 Fortran 90 中的行数? [英] How can I read the number of lines in Fortran 90 from a text file?

查看:23
本文介绍了如何从文本文件中读取 Fortran 90 中的行数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何读取文本文件中的行数?

How can I read the number of lines present in a text file?

我的文本文件似乎是这样的:

My text file seems to be like:

1
2
3
.
.
.
n

推荐答案

使用:

nlines = 0
OPEN (1, file = 'file.txt')
DO
    READ (1,*, END=10)
    nlines = nlines + 1
END DO
10 CLOSE (1)

print*, nlines

end

或者:

nlines = 0
OPEN (1, file = 'file.txt')
DO
  READ(1,*,iostat=io)
  IF (io/=0) EXIT
  nlines = nlines + 1
END DO
CLOSE (1)

print*, nlines

这篇关于如何从文本文件中读取 Fortran 90 中的行数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆