Fortran Coder

查看: 16921|回复: 10
打印 上一主题 下一主题

[文件读写] 做动力学结束后产生的文件读取问题

 关闭 [复制链接]

1967

帖子

12

主题

5

精华

论坛跑堂

臭石头雪球

F 币
1372 元
贡献
581 点

美女勋章热心勋章星光勋章新人勋章贡献勋章管理勋章帅哥勋章爱心勋章规矩勋章元老勋章水王勋章

楼主
发表于 2016-3-5 18:27:43 | 显示全部楼层
你这种数据结构,用派生类型会比较方便。

以下代码,你的问题我都在注释里说明了

[Fortran] 纯文本查看 复制代码
Program Readdata
  Implicit None
  Character (Len=64) :: str ! //修改为64
  Type :: OneRec
    Real (Kind=8) :: time, totenergy, poten, kinet, inter, temp
    Real (Kind=8) :: pressure, density, dipole
  End Type OneRec
  Integer :: ierr, count, i
  Type (OneRec), Allocatable :: myrec(:)
  Open (10, File='mdstat.txt', Status='old')
  Open (11, File='mdstat.tem', Status='unknown')
  count = 0 !// 初值为 0
  Do
    Read (10, '(a16)', Iostat=ierr) str ! //修改为16
    If (ierr/=0) Exit
    If (str(1:16)==' Simulation Time') count = count + 1 ! //前面加个空格
  End Do
  Allocate (myrec(count))
  Rewind (10)
  i = 1
  Do
    Read (10, '(a64)', Iostat=ierr) str
    If (ierr/=0) Exit
    If (str(1:16)==' Simulation Time') Then
      Read (str(22:), *) myrec(i)%time
      Read (10, '(a64)', Iostat=ierr) str
      Read (str(22:), *) myrec(i)%totenergy
      Read (10, '(a64)', Iostat=ierr) str
      Read (str(22:), *) myrec(i)%poten
      Read (10, '(a64)', Iostat=ierr) str
      Read (str(22:), *) myrec(i)%kinet
      Read (10, '(a64)', Iostat=ierr) str
      Read (str(22:), *) myrec(i)%inter
      Read (10, '(a64)', Iostat=ierr) str
      Read (str(22:), *) myrec(i)%temp
      Read (10, '(a64)', Iostat=ierr) str
      Read (str(22:), *) myrec(i)%pressure
      Read (10, '(a64)', Iostat=ierr) str
      Read (str(22:), *) myrec(i)%density
      Read (10, '(a64)', Iostat=ierr) str
      Read (str(22:), *) myrec(i)%dipole
      i = i + 1
    End If
  End Do
  Do i = 1, count
    Write (11, '(3x,9f14.4)') myrec(i)
  End Do
End Program Readdata
您需要登录后才可以回帖 登录 | 极速注册

本版积分规则

捐赠本站|Archiver|关于我们 About Us|小黑屋|Fcode ( 京ICP备18005632-2号 )

GMT+8, 2024-6-2 00:36

Powered by Tencent X3.4

© 2013-2024 Tencent

快速回复 返回顶部 返回列表