Fortran Coder

标题: 从文件指定行读取数据的方法 [打印本页]

作者: jojo9    时间: 2021-9-14 10:43
标题: 从文件指定行读取数据的方法
文件开头有说明性的字头,想从指定行读入数据有什么好的方法呢?
例如,要读入图中红框数据存到程序的整型数,读入蓝框数据到数组。
我的做法是直接顺序读取,再用循环跳过想跳过的行:
subroutine read_num
    integer i
    open(12, File = './file/faces')

    do i=1,9
        read(12, *)
    end do
   read(12, *) nface

end subroutine


请问有更好的实现方法嘛?

faces - 副本.txt

378 Bytes, 下载次数: 24


作者: 胡文刚    时间: 2021-9-14 10:56
[Fortran] 纯文本查看 复制代码
Subroutine read_num
  integer i
  character(len=256) :: cStr
  open(12, File = './file/faces')
  do
    read(12,"(a256)") cStr
    if(cStr(1:1)=="}")exit
  end do
  read(12, *) nface
  close(12)
End Subroutine read_num

作者: jojo9    时间: 2021-9-14 11:51
胡文刚 发表于 2021-9-14 10:56
[mw_shl_code=fortran,true]Subroutine read_num
  integer i
  character(len=256) :: cStr

学到了学到了,多谢您啦




欢迎光临 Fortran Coder (http://bbs.fcode.cn/) Powered by Discuz! X3.2