检查文件是否已经打开 [英] Check whether file has been opened already
问题描述
我正在编写一个文件读取库,需要检查文件是否已打开,以便可以跳过open语句并直接进行读取.
I am writing a file reading library, and need to check whether a file has been opened so that I can skip the open statement and go directly to a read.
这如何在fortran中实现?
How can this be achieved in fortran?
推荐答案
当想要了解与外部文件的连接时,可以使用 inquire
语句.有两种形式:按文件查询;按文件查询.按单位查询.
When one wants to know about connections to external files there is the inquire
statement. There are two forms to this: inquire by file; inquire by unit.
tom的答案显示按单位查询.这测试单元3是否连接到 any 文件.然后,可以继续使用 name =
和 named =
说明符询问连接文件的名称. 1
tom's answer shows inquire by unit. This tests whether unit 3 is connected to any file. One could then go on to ask the name of the connected file with the name=
and named=
specifiers.1
按文件查询可以询问:给定的文件是否连接到任何单元,如果可以,则连接到哪个单元?
Inquire by file allows one to ask: is a given file connected to any unit, and if so, to which unit?
inquire(file=filename, number=unit)
如果未连接文件,则 unit
将为 -1
,否则 unit
将与连接到文件的单元相对应.
If the file is not connected then unit
will be -1
, otherwise unit
will correspond to the unit connected to the file.
或者,根据您想对 open
语句执行的操作,对已经连接的文件进行 open
不一定是错误的.
Alternatively, depending on what you want to do with the open
statement, it isn't necessarily erroneous to open
with an already connected file.
[1]如果文件没有名称,则 name =
说明符中的变量将变为未定义.在未定义的情况下针对所需文件名测试此变量是不好的. named =
说明符允许检测到这种情况.
[1] The variable in the name=
specifier will become undefined if the file has no name. Testing this variable against the desired filename when it is undefined is bad. The named=
specifier allows detection of this case.
这篇关于检查文件是否已经打开的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!