在fortran中读取输入文件 [英] reading input file in fortran

查看:364
本文介绍了在fortran中读取输入文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要读取文件,例如:

NE                32         0
IBZINT             2
NKTAB            936
XC-POT    VWN       
ITER          29
MIX     2.00000000000000E-01
TOL     1.00000000000000E-05

我一直在寻找我正在寻找的index intrinsic,并相应地编写了代码:

I was thinking it is index intrinsic that I am looking for, and was writing a code accordingly:

编辑,代码已更新,

   Implicit None
    integer ::i,pos
    character(50) :: name
    character(len=16),dimension(100)::key,val
    key(1)="NE"
    open(12,file="FeRh/FeRh.pot_new",status="old")
    do i=1,100
      read(12,*)name
      if (name(1:2)==key(1))then
        write(*,*)"find NE"
        write(*,*)name(1:2)
        write(*,*)name(index("NE","")+21)
      endif
    end do
    close(12)
    !write(*,*)index(key(1),"")
    End Program  readpot

我希望第三条write语句中有32个. 在某些地方一定犯了可怕的错误.您能帮忙吗?

I am expecting to have 32 in the 3rd write statement. Must have gone horribly wrong some where. can you kindly help?

推荐答案

当您要从文件中读取一行时,将使用列表导向(*作为格式)输入.这不是您想要的,因为运行时将进行一些有限的解析.

When you want to read a line from the file you are using list-directed (* as the format) input. This isn't what you want as there will be some limited parsing by the run-time.

也就是说,第一个记录上的read(12,*) name将导致"NE"填充变量name中的许多空格,因为记录将在空格上分开.

That is, read(12,*) name on the first record will result in "NE" padded with lots of spaces in the variable name as the record will be split on the spaces.

要在name中使用整行,请在read中使用格式'(A)'.

As you want the entire line in name, use the format '(A)' in the read.

一旦有了这一行,就可以进行进一步的解析.但是,从您显示的内容来看index似乎无济于事,尤其是当您检查一个空的子字符串时.您知道键的长度(使用len_trim),因此,如果有匹配项,则知道第一个分隔符的位置.

Once you have that line, you can then do your further parsing. However, from what you show index doesn't seem to be helping, especially as you are checking against an empty substring. You know the length of the key (using len_trim) so if you have a match you know the location of the first separator.

这篇关于在fortran中读取输入文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆