Fortran读取混合的字符串和数字数据 [英] Fortran reading mixed string and numeric data

查看:489
本文介绍了Fortran读取混合的字符串和数字数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

读取声明时遇到问题。我更喜欢Fortran90,尽管其他版本可能是有可能的,如果它有帮助的话。我在一个文件中有一堆数据,可以描述为:


  • 空格分隔

  • 不是固定格式

  • 包含一个字符串后跟7个数字
  • 字符串包含正斜杠/ $ / $ b


下面是一个例子:

  maxele / OS1_HC_LF_A_0001_004_maxele.63 4.22 E-03 9.00E-01 1.00E-06 1 -999 -999 -999 
maxele / OS1_Inl_A_0001_005_maxele.63 2.11E-03 9.00E-01 1.00E-06 3 -999 -999 -999
maxele / OS1_HC_LF_C_0001_009_maxele.63 1.56E-03 9.00E-01 1.00E-06 2 58.77 -82.82 28.91
maxele / OS1_TS_B_0001_006_maxele.63 3.90E-03 9.00E-01 1.00E-06 1 -999 -999 -999

我已经了解到,如果遇到斜杠(/)字符,Fortran将停止读取语句无格式的读取,所以我必须使用格式指定IERS。由于字符串长度未知,我不确定如何确保字符串读取在第一个空格停止。我相信可以从整行读取,然后再解析,但这看起来很复杂。有没有办法强制它将数据视为空间分隔?提前致谢。

解决方案

你需要阅读整行和解析,在这种情况下它不那么糟糕,因为你只需要解析

  read(unit,'(a)')string!声明的时间足够长整行
iblnk = index(string,'')
read(string(iblnk:),*)seven_reals


I'm having trouble with a read statement. I'd prefer Fortran90, though other versions might be possible if it helps. I have a bunch of lines of data in a file that can be described as:

  • space-delimited
  • not in a fixed format
  • containing a string followed by 7 numbers
  • string contains a forward slash /

Here's an example:

maxele/OS1_HC_LF_A_0001_004_maxele.63    4.22E-03        9.00E-01        1.00E-06        1       -999    -999    -999
maxele/OS1_Inl_A_0001_005_maxele.63     2.11E-03        9.00E-01        1.00E-06        3       -999    -999    -999
maxele/OS1_HC_LF_C_0001_009_maxele.63  1.56E-03        9.00E-01        1.00E-06        2       58.77   -82.82  28.91
maxele/OS1_TS_B_0001_006_maxele.63   3.90E-03        9.00E-01        1.00E-06        1       -999    -999    -999  

I've learned that Fortran will stop a read statement if it encounters a slash (/) character for unformatted reads, so I have to use format specifiers. And because the string length is unknown, I'm not sure how to make sure the string read stops at the first space. I believe it's possible to read in the whole line, then parse afterwards, but this seems convoluted. Is there no way to force it to treat the data as space-delimited? Thanks in advance.

解决方案

you do need to read the whole line and parse, in this case its not so bad because you only need to parse the first string and can internal list read the rest.

read(unit,'(a)')string !declared long enough for a whole line
iblnk=index(string,' ')
read(string(iblnk:),*)seven_reals

这篇关于Fortran读取混合的字符串和数字数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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