无法检测文件是否存在 [英] Cannot detect file existing or not

查看:164
本文介绍了无法检测文件是否存在的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在一个我正在改进的程序中,我注意到Fortran没有检测到文件存在与否。这导致了一个尚未修复的逻辑错误。

  open(unit = nhist,file =历史,iostat = ierr)!如果文件不存在,该设置不能退出程序,因为ierr总是0 
if(ierr / = 0)then
write(*,*)'!!!错误#',ierr,' - 找不到转储文件'
stop
endif

!我用下面的语句,程序退出,即使文件已存在
打开(单位= nhist,file = history,err = 700)
700 ierr = -1
if(ierr / = 0)then
write(*,*)'!!!错误#',ierr,' - 未找到转储文件'
stop
endif


解决方案

这里有两个不同的问题。让我们分别看看他们。



首先,考虑

  open unit = nhist,file = history,iostat = ierr)

注释表明 ierr 始终设置为零。那么,为什么不应该设置为零? ierr 在出现错误的情况下应该不为零,但是文件不存在错误?



<不一定。在缺少 status = 说明符的情况下,将采用默认的 status ='unknown'。如果该文件不存在,编译器不必(并且不太可能)将这种情况下的开放视为错误。它可能会在写入时根据需要创建它,或者在尝试阅读时发出抱怨。



添加 status ='old'打开语句是通常说文件应该存在的方式。

第二,考虑

  open(单位= nhist,file = history,err = 700)
700 ierr = -1
if(ierr / = 0)then
...

如果这里有错误,执行将转移到标记为 700 的语句。从这个语句 ierr 被设置为一个非零值,如果结构处理该错误,则我们转到

只是标记为 700 的语句即使没有错误也会被执行:它只是下一个语句在打开之后,并且没有分支想念它。 [我可以举一个这样的分支的例子,但我不想鼓励在现代代码中使用 err = 。用工作 iostat = 是比较好的选择。]



但是如果你只是想测试一个文件,请考虑查询:

 逻辑迭代器
inquire(file = history,exists = itexists)
if(.not.itexists)error stopNo file :(

有些人会争论这比在 open 语句中使用 status ='old'还要好。


In a program I am improving, I noticed that Fortran does not detect file existing or not. This led to a logic error which has not been fixed. I highly appreciate if you could point out the problems or errors and give me your corrections.

  open(unit=nhist,file=history,iostat=ierr)!This setting cannot exit program if file does not exist because ierr is always 0
  if (ierr /=0) then
   write(*,*)'!!! error#',ierr,'- Dump file not found'
   stop
  endif

  !I used below statement, the program exits even though a file is existing
       open(unit=nhist,file=history,err=700)
   700 ierr=-1
       if (ierr /=0) then
       write(*,*)'!!! error#',ierr,'- Dump file not found'
       stop
       endif

解决方案

There are two distinct problems here. Let's look at them separately.

First, consider

open(unit=nhist,file=history,iostat=ierr)

The comment suggests that ierr is always set to zero. Well, why shouldn't it be set to zero? ierr should be non-zero in the case the case of an error, but is the file not existing an error?

Not necessarily. In the absence of a status= specifier the default status='unknown' is taken. The compiler doesn't have to (and is unlikely to) treat opening in this case as an error if the file doesn't exist. It's likely to create it as needed on a write, or complain when trying to read.

Adding status='old' to the open statement is the usual way of saying "the file should exist".

Second, consider

       open(unit=nhist,file=history,err=700)
   700 ierr=-1
       if (ierr /=0) then
       ...

If there's an error here, execution is transferred to the statement labelled 700. From this statement ierr is set to a non-zero value and off we go to the if construct to handle that error.

It's just that the statement labelled 700 also happens to be executed even without an error: it's simply the next statement after the open and there's no branch to miss it. [I could give an example of such branching, but I don't want to encourage use of err= in modern code. With the working iostat= things are far preferable.]

But if you just wish to test the existence of a file, consider inquire-by-file:

logical itexists
inquire (file=history, exist=itexists)
if (.not.itexists) error stop "No file :("

Some would argue this is even better than having status='old' in an open statement.

这篇关于无法检测文件是否存在的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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