Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Opening multiple files in Fortran 90

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?

like image 265
Vijay Avatar asked Aug 30 '25 16:08

Vijay


2 Answers

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.

like image 124
Deditos Avatar answered Sep 02 '25 11:09

Deditos


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
like image 43
William Pursell Avatar answered Sep 02 '25 10:09

William Pursell