24小时热门版块排行榜    

CyRhmU.jpeg
查看: 2297  |  回复: 3

lwingkei

禁虫 (小有名气)

本帖内容被屏蔽

» 猜你喜欢

» 本主题相关价值贴推荐,对您同样有帮助:

已阅   关注TA 给TA发消息 送TA红花 TA的回帖

zeppe

金虫 (小有名气)

【答案】应助回帖

★ ★ ★ ★ ★ ★ ★ ★ ★ ★
感谢参与,应助指数 +1
lwingkei: 金币+10, ★★★★★最佳答案, 很好,回头好好研究一下修改一下再用 2014-04-16 17:10:04
可以根据特征字符串所在行来定义flag进行限制,例如可以以"DM1"这个字符串所在行为起始行,以"DM2"这个字符串所在行为结束行,读入这之间的数据。其他DM3,...都类似的。
下面是读取"DM1"和"DM2"之间的数据的代码。
CODE:
! read a formatted file, see "example.txt"
      program TestReadFile
        implicit none

        character (len=80) :: filename
        integer, parameter :: fileunit=15
        logical :: bExist, bDM1, bDM2
        integer :: iOK, pos1, pos2
        character (len=80) :: line
        real :: var1, var2, var3

        ! name of input file
        filename="example.txt"

        ! check if file exist
        inquire (file=filename, exist=bExist)
        if (bExist) then
        else
          print *, filename, " not found!"
          stop
        endif

        ! open file
        open (unit=fileunit, file=filename, status='old', iostat=iOK)
        if (iOK /= 0) then
          print *, "can't open ", filename
          stop
        endif

        ! read file line by line
        do while (iOK == 0)
          read (fileunit, "(A)", iostat=iOK) line
         
          ! if line starts with "DM1"
          pos1=index(line, "DM1")
          if (pos1 == 1) then
            bDM1=.true.
            continue
          endif

          ! if line starts with "DM2"
          pos2=index(line, "DM2")
          if (pos2 == 1) then
            bDM1=.false.
            bDM2=.true.
            continue
          endif

          ! if in the 'DM1' block, read variables
          if ((bDM1) .and. (pos1 /= 1)) then
            read (line, *)var1, var2, var3
            print *, var1, var2, var3
          endif
         
        enddo

        ! close file
        close (fileunit)

      end program TestReadFile

2楼2014-04-16 13:07:31
已阅   关注TA 给TA发消息 送TA红花 TA的回帖

zeppe

金虫 (小有名气)

【答案】应助回帖

example.txt内容如下
DM1:
             0.02     -1.3     2.40
             1.50     -0.1     4.00
             3.02     -2.0     5.00
             0.60     0.30     3000
DM2:
             0.010     3000     1000
             0.020     3000     1000
             0.030     3000     1000
             0.040     3000     1000
             0.050     3000     1000
             0.060     3000     1000
DM3:
             1000     0.00     0.00
             2000     0.00     0.00
             3000     0.00     0.00
             4000     0.00     0.00
             5000     0.00     0.00
             6000     0.00     0.00
             7000     0.00     0.00
3楼2014-04-16 13:09:34
已阅   关注TA 给TA发消息 送TA红花 TA的回帖

tg2010

金虫 (小有名气)

【答案】应助回帖

感谢参与,应助指数 +1
先做个循环数有多少行。
第二个循环就按上面读出的行数来读。
4楼2014-04-16 15:31:41
已阅   关注TA 给TA发消息 送TA红花 TA的回帖
相关版块跳转 我要订阅楼主 lwingkei 的主题更新
信息提示
请填处理意见