无法检测文件是否存在 [英] Cannot detect file existing or not
问题描述
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
...
如果这里有错误,执行将转移到标记为 只是标记为 但是如果你只是想测试一个文件,请考虑查询: 有些人会争论这比在 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.
There are two distinct problems here. Let's look at them separately. First, consider The comment suggests that Not necessarily. In the absence of a Adding Second, consider If there's an error here, execution is transferred to the statement labelled It's just that the statement labelled But if you just wish to test the existence of a file, consider inquire-by-file: Some would argue this is even better than having 这篇关于无法检测文件是否存在的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋! 700
的语句。从这个语句 ierr
被设置为一个非零值,如果结构处理该错误,则我们转到 。
700
的语句即使没有错误也会被执行:它只是下一个语句在打开
之后,并且没有分支想念它。 [我可以举一个这样的分支的例子,但我不想鼓励在现代代码中使用 err =
。用工作 iostat =
是比较好的选择。]
逻辑迭代器
inquire(file = history,exists = itexists)
if(.not.itexists)error stopNo file :(
open
语句中使用 status ='old'
还要好。 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
open(unit=nhist,file=history,iostat=ierr)
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?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.status='old'
to the open
statement is the usual way of saying "the file should exist". open(unit=nhist,file=history,err=700)
700 ierr=-1
if (ierr /=0) then
...
700
. From this statement ierr
is set to a non-zero value and off we go to the if
construct to handle that error.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.]logical itexists
inquire (file=history, exist=itexists)
if (.not.itexists) error stop "No file :("
status='old'
in an open
statement.