在 Fortran 90 中打开多个文件
我想打开 10,000 个文件名从 abc25000
到 abc35000
的文件,并将一些信息复制到每个文件中。我编写的代码如下:
PROGRAM puppy
IMPLICIT NONE
integer :: i
CHARACTER(len=3) :: n1
CHARACTER(len=5) :: cnum
CHARACTER(len=8) :: n2
loop1: do i = 25000 ,35000 !in one frame
n1='abc'
write(cnum,'(i5)') i
n2=n1//cnum
print*, n2
open(unit=i ,file=n2)
enddo loop1
end
这段代码应该生成从 abc24000
开始到 abc35000
的文件,但它在中途停止了
文件 test-openFile.f90 第 17 行(单位 = 26021,文件 = '')
Fortran 运行时错误:打开的文件太多
我需要做什么来修复上述代码?
I would like to open 10,000 files with file names starting from abc25000
until abc35000
and copy some information into each file. The code I have written is as below:
PROGRAM puppy
IMPLICIT NONE
integer :: i
CHARACTER(len=3) :: n1
CHARACTER(len=5) :: cnum
CHARACTER(len=8) :: n2
loop1: do i = 25000 ,35000 !in one frame
n1='abc'
write(cnum,'(i5)') i
n2=n1//cnum
print*, n2
open(unit=i ,file=n2)
enddo loop1
end
This code is supposed to generate files starting from abc24000
until abc35000
but it stops about half way saying that
At line 17 of file test-openFile.f90 (unit = 26021, file = '')
Fortran runtime error: Too many open files
What do I need to do to fix the above code?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
此限制由您的操作系统设置。如果您使用的是 Unix/Linux 变体,则可以使用
ulimit -n
从命令行检查限制,并使用ulimit -n 16384
提高限制。您需要设置大于 10000 的限制,以允许 shell 打开的所有其他文件。您可能还需要管理员权限才能执行此操作。我经常将运行 Fortran 程序的限制提高到 2048,但从来不会高达 10000。但是,我同意其他答案,如果可能的话,最好重组程序以在打开下一个文件之前关闭每个文件。
This limit is set by your OS. If you're using a Unix/Linux variant, you can check the limit using from the command line using
ulimit -n
, and raise it usingulimit -n 16384
. You'll need to set a limit greater than 10000 to allow for all the other files that the shell will have open. You may also need admin privileges to do this.I regularly bump the limit up to 2048 to run Fortran programs, but never as high as 10000. However, I echo the other answers that, if possible, it's better to restructure your program to close each file before opening the next.
您需要一次处理一个文件(或以不超出操作系统限制的小组形式)。
You need to work on the files one at a time (or in small groups that do not exceed the limitation imposed by the operating system).
操作系统往往对资源有限制。通常,例如在 Linux 上,默认情况下每个进程的文件描述符限制为 1024 个。您收到的错误消息只是 Fortran 运行时库向上传递信息,表明由于操作系统错误而无法打开另一个文件。
Operating systems tend to have limits on resources. Typically on, for instance, Linux, there is by default a limit of 1024 file descriptors per process. The error message you're getting is just the Fortran runtime library passing information upwards that it was unable to open yet another file due to an OS error.