Opening multiple files in Fortran 90

空扰寡人 提交于 2019-12-01 07:52:46

问题


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?


回答1:


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).

for each file:
  open file
  write
  close file



回答2:


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 using ulimit -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.




回答3:


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.



来源:https://stackoverflow.com/questions/7611360/opening-multiple-files-in-fortran-90

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!