Fortran 90:读取一个带有一些“/”的泛型字符串人物 [英] Fortran 90: reading a generic string with enclosed some "/" characters

查看:402
本文介绍了Fortran 90:读取一个带有一些“/”的泛型字符串人物的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,我在一个简单的文件中发现了一些读未格式化字符串的问题。当找到第一个字符后,所有内容都会被忽略。



这是我想要阅读的文本示例:在前18个字符块被修复(从#Mod到Flow [kW]),在我正在编写的程序中有一个化学物种名称的列表,这些变量是变量(在本例中为5个)。

  #Mod ID Mod名称类型C. #Coll MF [kg / s] Pres。[Pa] Pres。[bar] Temp。[K] Temp。[C] Ent [ kJ / kg K]功率[kW] RPM [rad / s]热流量[kW]甲烷乙烷丙烷氮气H2O 



我想在经过一些正式检查后跳过前18个街区,然后阅读化学物种。为了做前者,我创建了一个尺寸为18的字符数组,每个字符数组的长度都为20.

  character(20) ,dimension(18):: chapp 

然后我想将这18个块与字符数组

  read(1,*)(chapp(i),i = 1,18)

...但这是结果:从chapp(1)到chapp(7)保存了右前7个字符串,但是这(8)

  chapp(8)='MF [kg'



从这里开始,一切都空了!



我怎么能克服这个阅读问题?

解决方案

这个问题是由于您使用了列表导向输入(*作为格式)。

你偶然发现一个怪癖:一个斜杠( / )会终止赋值给READ语句的输入列表。这正是您上面描述的行为。



这不是编译器编写器的选择,而是由所有相关的Fortran标准规定的。



解决方案是使用格式化输入。这有几个选项:

如果你知道你的标签总是在同一列中,你可以使用一个格式字符串,如' (1X,A4,2X,A2,1X,A3,2X)'(这不完整)读入各个标签。这很容易出错,如果写出数据的程序出于某种原因或者其他原因改变了格式,或者如果实验室是手工编辑的话,那也是不好的。

如果您可以控制写入标签的程序,则可以使用制表符分隔各个标签(以及之后的标签)。在整行中阅读,使用 INDEX 将其拆分为制表符分隔的子字符串,并使用(A)格式。不要使用列表导向的格式,否则你会被上面提到的 / 怪癖命中。这样做的好处是您的标签也可以包含空格,并且数据可以很容易地从/导入Excel。这是我通常在这种情况下做的。



否则,您可以阅读整行并拆分多个空格。比在单个制表符中分割要复杂一点,但如果无法控制数据源,它可能是最好的选择。你不能有包含空格的标签。


Hy everybody, I've found some problems in reading unformatted character strings in a simple file. When the first / is found, everything is missed after it.

This is the example of the text I would like to read: after the first 18 character blocks that are fixed (from #Mod to Flow[kW]), there is a list of chemical species' names, that are variables (in this case 5) within the program I'm writing.

 #Mod  ID Mod  Name       Type C.  #Coll    MF[kg/s]    Pres.[Pa]    Pres.[bar]    Temp.[K]    Temp.[C]    Ent[kJ/kg K]    Power[kW]    RPM[rad/s]    Heat Flow[kW]     METHANE     ETHANE      PROPANE    NITROGEN    H2O       

I would like to skip, after some formal checks, the first 18 blocks, then read the chemical species. To do the former, I created a character array with dimension of 18, each with a length of 20.

character(20), dimension(18)       :: chapp

Then I would like to associate the 18 blocks to the character array

read(1,*) (chapp(i),i=1,18)

...but this is the result: from chapp(1) to chapp(7) are saved the right first 7 strings, but this is chapp(8)

chapp(8) = 'MF[kg               '

and from here on, everything is leaved blank!

How could I overcome this reading problem?

解决方案

The problem is due to your using list-directed input (the * as the format). List-directed input is useful for quick and dirty input, but it has its limitations and quirks.

You stumbled across a quirk: A slash (/) in the input terminates assignment of values to the input list for the READ statement. This is exactly the behavior that you described above.

This is not choice of the compiler writer, but is mandated by all relevant Fortran standards.

The solution is to use formatted input. There are several options for this:

If you know that your labels will always be in the same columns, you can use a format string like '(1X,A4,2X,A2,1X,A3,2X)' (this is not complete) to read in the individual labels. This is error-prone, and is also bad if the program that writes out the data changes format for some reason or other, or if the labes are edited by hand.

If you can control the program that writes the label, you can use tab characters to separate the individual labels (and also, later, the labels). Read in the whole line, split it into tab-separated substrings using INDEX and read in the individual fields using an (A) format. Don't use list-directed format, or you will get hit by the / quirk mentioned above. This has the advantage that your labels can also include spaces, and that the data can be imported from/to Excel rather easily. This is what I usually do in such cases.

Otherwise, you can read in the whole line and split on multiple spaces. A bit more complicated than splitting on single tab characters, but it may be the best option if you cannot control the data source. You cannot have labels containing spaces then.

这篇关于Fortran 90:读取一个带有一些“/”的泛型字符串人物的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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