读取直接访问文件时,GFortran I / O错误5002 [英] GFortran I/O error 5002 while reading a direct access file

查看:290
本文介绍了读取直接访问文件时,GFortran I / O错误5002的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Fortran中读取文件时遇到一些问题。正如你所看到的,我正在浏览一个读取特定长度的记录的文件。



会发生什么事情,当涉及某个记录时,我得到一个IOSTAT错误5002.现在我的问题是这个错误是什么意思:是文件结束还是没有记录或其他东西?我可以忽略它吗?



我使用MinGW GFortran 4.8.0。



以下是代码:

  PROGRAM test_read 

INTEGER * 4 HCM_error
DOUBLE PRECISION N_Record(22)
CHARACTER * 8 C_Record(22)

EQUIVALENCE(N_Record,C_Record)

OPEN(UNIT = 11,FILE ='C:/BORDER/D__HOL.000',STATUS= 'OLD',ACCESS ='DIRECT',RECL = 176,ACTION ='READ',IOSTAT = IOS)

HCM_error = 0

DO N_rec = 1,200
READ(11,REC = N_rec,IOSTAT = IOS)C_Record

WRITE(*,*)|,IOS,'',N_rec,'',N_record(21),' ,N_record(22), |

!到达文件结尾(或不存在的记录)? (IOS .LT。0).OR。(IOS .EQ。36))退出

IF(IOS .NE。0)THEN

HCM_Error = 1049
EXIT
END IF
END DO

CLOSE(UNIT = 11)

WRITE(*,*)HCM_error
END PROGRAM


解决方案

iostat = 指定符返回的非零值在编译器中不可移植。如果你想确定一个特定的代码是什么意思,那么你有两个选择:


  • 阅读编译器的文档(如果存在的话)

  • 使用带有字符变量的 iomsg = 说明符



在这种情况下,当您尝试 iomsg = 时,您收到消息不存在记录编号。所以,问题解决了。

好吧,差不多。还有更多要说的。

您可能会感到惊讶,因为您正在直接访问记录,但在达到无记录状态时未先达到文件结束状态。您正在测试(IOS .LT。0),并发表评论!文件已到达。



当读取直接访问连接的文件时,不会出现文件结尾条件。



您可以如何检测记录不是有效的数字文件的结尾?并不多,便携式,但任何来自 iostat = 的正数表示错误情况。不过,你现在知道,这个特定的 5002 的含义是什么。






我还可以补充说,只有在没有成功的情况下, iomsg = 的字符变量才由传输语句定义。只有当你知道转移失败时才考虑它。


I have a little Problem reading a file in Fortran. As you can see I am lopping over a file reading certain records with a specific length.

What happens is, when it comes to a certain record I'm getting an IOSTAT Error 5002. Now my question is what does this error mean: is it end of file or there is no record left or something else? Can I ignore it?

I am using MinGW GFortran 4.8.0.

Here's the code:

PROGRAM test_read

INTEGER*4           HCM_error
DOUBLE PRECISION    N_Record(22)
CHARACTER*8         C_Record(22)

EQUIVALENCE         (N_Record,C_Record)

OPEN (UNIT=11, FILE='C:/BORDER/D__HOL.000',STATUS='OLD', ACCESS='DIRECT',RECL=176, ACTION='READ', IOSTAT=IOS)

HCM_error=0

DO N_rec = 1, 2000
    READ (11, REC=N_rec, IOSTAT=IOS) C_Record

    WRITE(*,*) "|",IOS,' ',N_rec,' ',N_record(21),' ',N_record(22),"|"

    !End of file reached (or non existing record) ?
    IF ((IOS .LT. 0) .OR. (IOS .EQ. 36)) EXIT

    IF (IOS .NE. 0) THEN
        !Error in (border-) line data
        HCM_Error = 1049
        EXIT
    END IF
END DO

CLOSE(UNIT=11)

WRITE (*,*) HCM_error 
END PROGRAM

解决方案

The non-zero values returned by an iostat= specifier are not portable across compilers. If you wish to determine what a particular code means then you have two options:

  • read the compiler's documentation (if it exists)
  • use the iomsg= specifier with a character variable

In this case, when you tried iomsg= you got the message "Non-existing record number". So, problem solved.

Well, almost. There's more to say.

You may be surprised that you are going through records in turn in direct access, but are reaching a "no record" state without first reaching an "end of file" state. You are testing (IOS .LT. 0) with a comment "!End of file reached".

When reading a file connected for direct access, the end of file condition doesn't arise.

What can you do to detect that the record isn't a valid number, beyond the end of the file? Not much, portably, but any positive number from iostat= indicates an error condition. You know now, though, what this particular 5002 means.


I should probably also add that the character variable for iomsg= is defined by the transfer statement only if there isn't success. Consider it only if you know the transfer failed.

这篇关于读取直接访问文件时,GFortran I / O错误5002的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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