简体   繁体   English

在fortran中读取输入文件

[英]reading input file in fortran

I am looking for reading a file, like: 我正在寻找读取文件,例如:

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

I was thinking it is index intrinsic that I am looking for, and was writing a code accordingly: 我以为我正在寻找index intrinsic的,并据此编写代码:

EDIT The code is updated, 编辑代码已更新,

   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

I am expecting to have 32 in the 3rd write statement. 我希望在第三个write语句中有32个。 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. 这不是您想要的,因为运行时将进行一些有限的解析。

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. 也就是说,第一个记录上的read(12,*) name将导致"NE"在变量name填充很多空格,因为记录将在空格上拆分。

As you want the entire line in name , use the format '(A)' in the read . 如要在name显示整行,请在read使用格式'(A)'

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. 但是,从显示的内容看, index似乎无济于事,尤其是当您检查一个空的子字符串时。 You know the length of the key (using len_trim ) so if you have a match you know the location of the first separator. 您知道密钥的长度(使用len_trim ),因此,如果有匹配项,则知道第一个分隔符的位置。

If I wanted to read a line such as 如果我想读一行

NE                32         0

I'd write a statement such as 我会写一个像

read(12,*) name, int1, int2

and expect my processor to set name to NE , int1 to 32 and int2 to 0 , if, that is, I'd declared int1 and int2 to be integers. 并希望我的处理器将name设置为NE ,将int132 ,将int20 ,如果是的话,我将int1int2声明为整数。

I'm puzzled that you seem to want to read a line of text and then parse it, all the while ignoring the benefits of list-directed input. 让我感到困惑的是,您似乎想读一行文本然后对其进行解析,而同时却忽略了列表导向输入的好处。 If you do want to parse it into something other than a character variable and two integers, let us know. 如果您确实想将其解析为字符变量和两个整数之外的其他内容,请告诉我们。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM