# How to manage Fortran file units in Julia?

**URL:** <https://discourse.julialang.org/t/how-to-manage-fortran-file-units-in-julia/116688>\
**Category:** General Usage\
**Tags:** fortran, interoperability\
**Created:** [July 6, 2024, 2:49am UTC](https://discourse.julialang.org/t/how-to-manage-fortran-file-units-in-julia/116688 "2024-07-06T02:49:35Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Abhro](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/abhro/32/220753_2.png) [@Abhro](https://discourse.julialang.org/u/Abhro)\
**Post date:** [July 6, 2024, 2:49am UTC](https://discourse.julialang.org/t/how-to-manage-fortran-file-units-in-julia/116688/1 "2024-07-06T02:49:35Z")

</div>

I’m calling a Fortran subroutine through `@ccall` which is declared as

```fortran
subroutine data_input(..., fileunit)
integer, intent(out) :: fileunit
...
open(newunit = fileunit, ...)
...
end subroutine

```

And various other Fortran subroutines that take the unit as an `integer` to write to the file. My questions are

- How do I convert the file unit number to a Julia IO stream? (Also, if the filename is not known, and the subroutine just returns the unit number)
- How do I pass in a file IO stream opened in Julia as a file unit to a Fortran subroutine? (If i want to divert the use of the subroutine and manage the file handles within Julia)
- How do I make sure that a file unit opened by a subroutine stays open so that when the `integer` is passed to other subroutines it refers to the same file?

---

<div class="post-metadata">

**Author:** ![mkitti](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mkitti/32/12459_2.png) [@mkitti](https://discourse.julialang.org/u/mkitti)\
**Post date:** [July 6, 2024, 3:01am UTC](https://discourse.julialang.org/t/how-to-manage-fortran-file-units-in-julia/116688/2 "2024-07-06T03:01:47Z")

</div>

The first question is how would you convert between the Fortran and C counterparts?

My thought is that this is probably a bad idea.

---

<div class="post-metadata">

**Author:** ![Abhro](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/abhro/32/220753_2.png) [@Abhro](https://discourse.julialang.org/u/Abhro)\
**Post date:** [July 6, 2024, 3:04am UTC](https://discourse.julialang.org/t/how-to-manage-fortran-file-units-in-julia/116688/3 "2024-07-06T03:04:21Z")

</div>

It probably _is_ a bad idea, but I’m dealing with a lot of legacy code, and short of rewriting it, which would take a lot of time and effort, I have to “Frankenstein” together an approach. I know Fortran file units don’t exactly correspond to file descriptors in UNIX either, so I’m also lost between the file unit ↔ file descriptor conversion 😅

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [July 6, 2024, 1:06pm UTC](https://discourse.julialang.org/t/how-to-manage-fortran-file-units-in-julia/116688/4 "2024-07-06T13:06:38Z")

</div>

> [@Abhro](#):
>
> How do I convert the file unit number to a Julia IO stream? (Also, if the filename is not known, and the subroutine just returns the unit number)

If you are using the GNU Fortran Compiler, you can call [`FNUM`](https://gcc.gnu.org/onlinedocs/gfortran/FNUM.html) (in Fortran) to convert a file number to a POSIX file descriptor, and given that you can call [`fdio`](https://docs.julialang.org/en/v1/base/io-network/#Base.fdio) in Julia to get an `IO` object.

(This is a GNU extension; other compilers may or may not have similar extensions. Intel Fortran has a similar function called [`PXFFILENO`](https://www.intel.com/content/www/us/en/docs/fortran-compiler/developer-guide-reference/2023-2/pxffileno.html), and Oracle/Sun Fortran has [`getfd`](https://docs.oracle.com/cd/E19957-01/805-4942/6j4m3r8u2/index.html).)

> [@Abhro](#):
>
> How do I pass in a file IO stream opened in Julia as a file unit to a Fortran subroutine? (If i want to divert the use of the subroutine and manage the file handles within Julia)

I can’t find any inverse of `FNUM`, so you may have to open all the files on the Fortran side and then convert them to Julia `IO` objects as needed, rather than the reverse.

> [@Abhro](#):
>
> How do I make sure that a file unit opened by a subroutine stays open so that when the `integer` is passed to other subroutines it refers to the same file?

Don’t close it? `OPEN` and `CLOSE` are managed manually in Fortran, as I understand it, so you can do whatever you want.

> [@Abhro](#):
>
> I’m dealing with a lot of legacy code, and short of rewriting it, which would take a lot of time and effort, I have to “Frankenstein” together an approach.

I’ve been there, so I feel your pain. Ideally, you could port as much of the I/O as possible first, so that you are doing all of your I/O on the Julia side and just calling Fortran for computational routines.
