Search code examples
stringiofortran

Get file unit by name and vice versa in Fortran


Is there a way in Fortran 90 to get a file unit by name and file name by unit number? I know it will be expensive, but I have a lot of file names that I want to be able to re-access in a subroutine after they've been created..


Solution

  • A command like

    inquire(unit=12, opened=openedq, file=name_of_file)
    

    will return the name of the file connected to unit 12 to the character variable name_of_file. If there is no file connected to the unit opened will return false, otherwise true. The command takes a variety of other optional arguments too.

    To get the unit number of a file which is already attached use a variation on the preceding, like this

    inquire(file='my_favourite_file.nuts', number=unknown_unit_number, opened=openedq)
    

    Reference to your compiler documentation should explain all the variations that inquire supports.