在IOSTAT的正值之后,我可以读取行的其余部分吗? [英] Can I read the rest of the line after a positive value of IOSTAT?

查看:264
本文介绍了在IOSTAT的正值之后,我可以读取行的其余部分吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含13列和41行的文件,其中包含 Joback Method 的系数为41个不同的组。一些值是不存在的,表格将它们列为X。我将表保存为.csv,并在我的代码中将文件读取到数组。 .csv中的两行文本(第二行包含非现有系数)如下所示:

I have a file with 13 columns and 41 lines consisting of the coefficients for the Joback Method for 41 different groups. Some of the values are non-existing, though, and the table lists them as "X". I saved the table as a .csv and in my code read the file to an array. An excerpt of two lines from the .csv (the second one contains non-exisiting coefficients) looks like this:

48.84,11.74,0.0169,0.0074,9.0,123.34,163.16,453.0,1124.0,-31.1,0.227,-0.00032,0.000000146
X,74.6,0.0255,-0.0099,X,23.61,X,797.0,X,X,X,X,X 

我试过的是读取和定义一个数组来保存每个IOSTAT值,所以我可以知道是否读取了一个X(即IOSTAT是肯定的):

What I've tried doing was to read and define an array to hold each IOSTAT value so I can know if an "X" was read (that is, IOSTAT would be positive):

DO I = 1, 41
        (READ(25,*,IOSTAT=ReadStatus(I,J)) JobackCoeff, J = 1, 13)
END DO

我发现的问题是,如果要读取的行的第一个值是X,产生一个正值ReadStatus,的那些值的值不能正确读取。

The problem, I've found, is that if the first value of the line to be read is "X", producing a positive value of ReadStatus, then the rest of the values of those line are not read correctly.

我的目的是使用ReadStatus数组来产生错误信息,如果JobackCoeff(I,J)引起读取错误,因此精确定位X。

My intent was to use the ReadStatus array to produce an error message if JobackCoeff(I,J) caused a read error, therefore pinpointing the "X"s.

我可以强制程序在读取错误后继续读取行吗?

Can I force the program to keep reading a line after there is a reading error? Or is there a better way of doing this?

推荐答案

一旦在输入执行期间发生错误,那么处理输入列表终止。此外,在输入列表中指定的所有变量都是未定义的。你的第一个问题的简短答案是:不,没有办法在阅读错误后继续读一行。

As soon as an error occurs during the input execution then processing of the input list terminates. Further, all variables specified in the input list become undefined. The short answer to your first question is: no, there is no way to keep reading a line after a reading error.

我们来,需要更复杂的输入处理:将行读入字符变量并处理。我不会为你编写完整的代码(主要是因为它不确切是什么需要),但是当你有一个字符变量,你可能会发现 index 内在有用。这样,你可以找到 X (重复调用子字符串,找到它们的所有行)。

We come, then, to the usual answer when more complicated input processing is required: read the line into a character variable and process that. I won't write complete code for you (mostly because it isn't clear exactly what is required), but when you have a character variable you may find the index intrinsic useful. With this you can locate Xs (with repeated calls on substrings to find all of them on a line).

或者,如果您提供一个显式格式(而不是依赖于列表导向( fmt = * )输入),您可以能够对非前进输入 advance ='no'语句中)。然而,一旦出现错误情况,文件的位置就变得不确定:你还必须处理这个。

Alternatively, if you provide an explicit format (rather than relying on list-directed (fmt=*) input) you may be able to do something with non-advancing input (advance='no' in the read statement). However, as soon as an error condition comes about then the position of the file becomes indeterminate: you'll also have to handle this. It's probably much simpler to process the line-as-a-character-variable.

下面给出概念的概要(没有声明,鲁棒性):

An outline of the concept (without declarations, robustness) is given below.

read(iunit, '(A)') line
idx = 1    
do i=1, 13
  read(line(idx:), *, iostat=iostat) x(i)
  if (iostat.gt.0) then
    print '("Column ",I0," has an X")', i
    x(i) = -HUGE(0.)  ! Recall x(i) was left undefined
  end if
  idx = idx + INDEX(line(idx:), ',')
end do     

这篇关于在IOSTAT的正值之后,我可以读取行的其余部分吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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