Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.
29235 Discussions

Is there a way to close all opened files at once?

OP1
New Contributor III
2,482 Views
Hi,

I have a DLL which opensa series of files (the list of files depends on the operations being executed and cannot be predicted in advance).
When an error condition occurs, I would like to be able to close all the files which have been opened by the DLL.

I suppose that doing a DO loop on unit numbers from 1 to a-very-big number and closing the opened units might work; but it certainly is not very elegant (or efficient).

Is there a way to close all the opened files at once?

Thanks,

Olivier
0 Kudos
6 Replies
GVautier
New Contributor III
2,482 Views
Hello

When the process terminates, all opened files are closed.
0 Kudos
OP1
New Contributor III
2,482 Views
Keep in mind that this is a DLL; If an error condition occurs (and is handled by the DLL), the DLL returns the execution to the main program, which in turns calls the DLL again (hopefully, with new parameters that do not trigger an error). So, some files might already be opened for the second call to the DLL.

Olivier
0 Kudos
mecej4
Honored Contributor III
2,482 Views
Is there some reason why you do not find it desirable to maintain a list of open units, and/or to restrict the unit numbers to, say, < 100 ?
0 Kudos
John4
Valued Contributor I
2,482 Views
In that case, why not just making the DLL check every unit before opening the file? You can write a procedure to do just that and call it right before the OPEN statement. Something like:


call close_file_if_open (foo_unit)

open (unit = foo_unit, file = ...

0 Kudos
Steven_L_Intel1
Employee
2,482 Views
There is no option to close all open files, nor is there a convenient way to get a list of units that are open. (You can loop from 0 to 21473642(or something like that) and use INQUIRE (UNIT=n,OPENED=) to see if a unit is opened, but I don't recommend that.

I agree with the suggestion that you keep track of the unit numbers you use to open files.
0 Kudos
OP1
New Contributor III
2,482 Views

Thanks to all for your answers and suggestions. It seems there is no easy shortcut. I'll have to implement a simple file tracking mechanism then.

Olivier

0 Kudos
Reply