3

我需要关闭并删除几个文件,一旦它们打开并且内容存储在一些变量中。为了避免重复语法,我可以使用:

        OPEN(UNIT = 27, FILE = "C:/Abaqus_JOBS/w.txt", status = "UNKNOWN")
  C
        READ(UNIT,END=1000) w
   1000 CLOSE (UNIT, status='delete') ,

这样我只需要指定CLOSE (UNIT, status='delete')一次?

谢谢

4

2 回答 2

3

我个人不会使用 end 语句,而是调用一个关闭正确文件的子例程:

subroutine del_file(uFile, stat)
  implicit none
  integer uFile, stat 

c If the unit is not open, stat will be non-zero
  close(unit=uFile, status='delete', iostat=stat)
end subroutine

您已阅读声明,然后将是:

read(unit=curUnit, iostat=stat) w
if ( stat .ne. 0 ) call del_file(curUnit, stat)

你仍然需要一些逻辑来不从关闭的文件中读取。我会推荐一个数组来保存与输入文件对应的所有单元。

于 2013-10-21T18:03:31.767 回答
1

你可以,但你会在两者之间做所有事情,1000 CLOSE...然后再做一遍。那是,

       READ(UNIT=27, END=1000)
1000   CLOSE(STATUS='delete')
       ... computations ...
       READ(UNIT=28, END=1000)

会导致你重新做... computations ...一遍,这可能是你想要的。显式编写命令可能会更容易,或者CLOSE正如 Alexander 在评论中所说,编写一个关闭文件的函数,给定特定的文件 ID,然后将其删除:

FUNCTION FileClose(lun) RESULT(ierr)
   INTEGER :: lun, ierr
   CLOSE(lun, STATUS='delete', IOSTAT=ierr)
END FUNCTION
于 2013-10-21T18:02:53.140 回答