使用 Fortran 中的文件命名格式打开多个文件进行读取
Open multiple files for reading using the file naming format in Fortran
我需要帮助打开多个文件以便用 Fortran 逐一阅读。下面的代码具有正确的文件名称,但在打开之前会覆盖文件的内容。
我怎样才能阻止这种情况发生
WRITE(FN,10)lam, zeta, (array(k)%str)!,k=1,N)
WRITE(6,*)FN!filename
OPEN(1,FILE=FN, status='replace')
CLOSE(1)
10 FORMAT('4e3_2048_',(I3.0),'_',(I2.2),'_',(A3),'.ksz_cl.txt') !
当您在打开文件时使用 status='replace'
会导致文件被删除并重新创建 (Fortran 2018 12.5.6.18):
If REPLACE is specified and the file does exist, the file is deleted, a new file is created with the same name, and the status is changed to OLD.
当你想从文件中读取时,这不好。相反,使用类似
open(1, file=FN, action='read', status='old', position='rewind')
确保:文件存在;它已打开供阅读;位于文件的开头。
我看到status='replace'
意在表示连接被替换,允许单元号被重复使用。可以看出,这是不正确的。请注意:一旦连接关闭,就可以愉快地重复使用单元号。事实上,如果一个 open
语句引用一个已经连接到不同文件的单元,那么在第一个连接上就有一个隐含的 close
。
我需要帮助打开多个文件以便用 Fortran 逐一阅读。下面的代码具有正确的文件名称,但在打开之前会覆盖文件的内容。
我怎样才能阻止这种情况发生
WRITE(FN,10)lam, zeta, (array(k)%str)!,k=1,N)
WRITE(6,*)FN!filename
OPEN(1,FILE=FN, status='replace')
CLOSE(1)
10 FORMAT('4e3_2048_',(I3.0),'_',(I2.2),'_',(A3),'.ksz_cl.txt') !
当您在打开文件时使用 status='replace'
会导致文件被删除并重新创建 (Fortran 2018 12.5.6.18):
If REPLACE is specified and the file does exist, the file is deleted, a new file is created with the same name, and the status is changed to OLD.
当你想从文件中读取时,这不好。相反,使用类似
open(1, file=FN, action='read', status='old', position='rewind')
确保:文件存在;它已打开供阅读;位于文件的开头。
我看到status='replace'
意在表示连接被替换,允许单元号被重复使用。可以看出,这是不正确的。请注意:一旦连接关闭,就可以愉快地重复使用单元号。事实上,如果一个 open
语句引用一个已经连接到不同文件的单元,那么在第一个连接上就有一个隐含的 close
。