Fortran Coder

查看: 2395|回复: 2

[文件读写] fortran读取txt文件并赋值

[复制链接]

5

帖子

2

主题

0

精华

新人

F 币
25 元
贡献
12 点
发表于 2022-12-22 17:03:58 | 显示全部楼层 |阅读模式
   想请教一下各位大佬,我想用Fortran读取txt文件,一共15行,第一行代表有几组数据,一组数据有七行,然后每七行进行赋值怎么实现,能不能帮我写一下
        module typ_vis        implicit none
        type :: vis
          integer::nlink  !表示第几组
          real*8 :: c1
          real*8 :: c2
          real*8 :: a1
          real*8 :: a2
          real*8 :: d1
          real*8 :: d2    !spring parameters
        end type vis
      end module typ_vis

       subroutine ReadLinkProp()
        use typ_vis
        implicit none
        integer::I
        integer::nOfsprng  !表示一共几组
        type(visdamper),allocatable::AllSprngProps(:)
        if (.not.allocated(AllSprngProps)) then
            allocate(AllSprngProps(100))
        end if

                        open(55,file='vis.txt')
              read(55,*) nOfsprng   !读取一共多少组
                        !write(*,*) "nOfSprng", nOfSprng
              do I=1, nOfSprng
              read(55,*) AllSprngProps(I)%nlink,  !读取第几组
     1                 AllSprngProps(I)%c1,
     2                 AllSprngProps(I)%c2,
     3                 AllSprngProps(I)%a1,
     4                 AllSprngProps(I)%a2,
     5                 AllSprngProps(I)%d1,
     6                 AllSprngProps(I)%d2,
              end do
                        close(55)

        return
      end subroutine ReadLinkProp
(txt文件如下:
     2  !表示两组数据
      1!表示第一组
      300
      600
      0.2
      0.3
      0.01
      0.02
      2!表示第二组
      400
      700
      0.4
      0.5
      0.03
      0.04)

155

帖子

2

主题

1

精华

大师

Vim

F 币
944 元
贡献
461 点

规矩勋章

发表于 2022-12-23 11:11:15 | 显示全部楼层
[Fortran] 纯文本查看 复制代码
         type(visdamper),allocatable::AllSprngProps(:)

改为
[Fortran] 纯文本查看 复制代码
         type(vis),allocatable::AllSprngProps(:)

5

帖子

2

主题

0

精华

新人

F 币
25 元
贡献
12 点
 楼主| 发表于 2022-12-23 15:57:46 | 显示全部楼层

想请教一下我这样写可以吗?其他地方有没有逻辑和语法错误呢?
您需要登录后才可以回帖 登录 | 极速注册

本版积分规则

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

GMT+8, 2024-3-29 03:56

Powered by Tencent X3.4

© 2013-2024 Tencent

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