fortran - 如何从文本文件中读取 Fortran 90 中的行数?

标签 fortran

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

我的文本文件看起来像:

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 - 如何从文本文件中读取 Fortran 90 中的行数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30692424/

相关文章:

c++ - Fortran 中的指针和 C++ 中的指针有什么区别

r - Fortran 子例程不适用于 R 包

python - 在Red Hat Enterprise Linux Server版本6.10(圣地亚哥)上构建MultiNest时,安装失败

arrays - 在 Fortran : Incompatible ranks 0 and 1 in assignment 中使用 MINLOC

compiler-construction - 使用ifort 11.0编译时出现语法错误

fortran - 何时使用 iso_Fortran_env 、selected_int_kind、real(8) 或 -fdefault-real-8 编写或编译 Fortran 代码?

ubuntu - 在 Ubuntu 20.04 上编译和运行 Xrotor

fortran - 将标量和数组元素传递给需要数组的过程

fortran - 当仅将 C_LOC 地址返回给 C 程序时,Fortran 变量需要 SAVE 属性吗?

Fortran INTENT 属性,带有带向量下标的实参