# Connecting a file to different units for simultaneous input and output

**URL:** <https://fortran-lang.discourse.group/t/connecting-a-file-to-different-units-for-simultaneous-input-and-output/3069>\
**Category:** Uncategorized\
**Created:** [March 30, 2022, 2:39am UTC](https://fortran-lang.discourse.group/t/connecting-a-file-to-different-units-for-simultaneous-input-and-output/3069 "2022-03-30T02:39:39Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Beliavsky](https://avatars.discourse-cdn.com/v4/letter/b/ba8739/32.png) [@Beliavsky](https://fortran-lang.discourse.group/u/Beliavsky)\
**Post date:** [March 30, 2022, 2:39am UTC](https://fortran-lang.discourse.group/t/connecting-a-file-to-different-units-for-simultaneous-input-and-output/3069/1 "2022-03-30T02:39:39Z")

</div>

[Modern Fortran Explained: Incorporating Fortran 2018](https://oxford.universitypressscholarship.com/view/10.1093/oso/9780198811893.001.0001/oso-9780198811893) says on p278

> A file already connected to one unit must not be specified for connection to another unit.

The [Fortran 2018 standard](https://j3-fortran.org/doc/year/18/18-007r1.pdf) merely says in section 12.5.4 that the results of doing so are compiler-dependent:

> It is processor dependent whether a file can be connected to more than one unit at the same time.
> 
> …
> 
> If input/output operations are performed on more than one unit while they are connected to the same external the results are processor dependent.

Below is a code that simultaneously opens a file for reading and writing. The results by compiler are listed in the comments. Gfortran and ifort both allow a file to be read in one unit as it is written in another. Gfortran requires a FLUSH statement for this to work, but ifort does not. Flang and g95 give an error message when a file is connected to more than one unit. I think all of the compilers are standard-conforming.

```auto
program flush_file
implicit none
integer :: i,iter
integer, parameter :: n = 10**3, inu = 10, outu = 11
character (len=*), parameter :: data_file = "random.txt"
real, allocatable :: x(:),y(:)
logical :: flushed
allocate (x(n),y(n))
call random_number(x)
print "(6a15)","flushed","minval(x)","maxval(x)","sum(abs(x-y))"
do iter=1,2
   flushed = iter == 1
   open (unit=outu,file=data_file,action="write")
   open (unit=inu,file=data_file,action="read",status="old")
   do i=1,n ! write and read data from same file
      write (outu,*) x(i)
      if (flushed) flush(outu)
      read (inu,*) y(i)
   end do
   print "(l15,5f15.4)",flushed,minval(x),maxval(x),sum(abs(x-y))
   close (outu)
   close (inu)
end do
end program flush_file
! sample ifort output:
! flushed minval(x) maxval(x) sum(abs(x-y))
! T 0.0000 0.9957 0.0000
! F 0.0000 0.9957 0.0000
! sample gfortran output:
! flushed minval(x) maxval(x) sum(abs(x-y))
! T 0.0005 0.9993 0.0000
! At line 18 of file flush.f90 (unit = 10, file = 'random.txt')
! Fortran runtime error: End of file
! flang output:
! flushed minval(x) maxval(x) sum(abs(x-y))
! FIO-F-207/OPEN/unit=10/file is already connected to another unit.
! File name = random.txt
! In source file flush.f90, at line number 14
! g95 output: same as flang

```

Depending on the compilers that you use, maybe it’s ok to connect a file to different units for both input and output?

---

<div class="post-metadata">

**Author:** ![Arjen](https://avatars.discourse-cdn.com/v4/letter/a/b9bd4f/32.png) [@Arjen](https://fortran-lang.discourse.group/u/Arjen)\
**Post date:** [March 30, 2022, 7:28am UTC](https://fortran-lang.discourse.group/t/connecting-a-file-to-different-units-for-simultaneous-input-and-output/3069/2 "2022-03-30T07:28:36Z")

</div>

Reading and writing to the same file at the “same” time is a bit tricky, but how about reading from the file vai two different units? If you want different pieces of information from the same file, it may be useful to have it opened at several units, so that the two tasks can proceed without interfering with each other.

---

<div class="post-metadata">

**Author:** ![msz59](https://avatars.discourse-cdn.com/v4/letter/m/3d9bf3/32.png) [@msz59](https://fortran-lang.discourse.group/u/msz59)\
**Post date:** [March 30, 2022, 9:15am UTC](https://fortran-lang.discourse.group/t/connecting-a-file-to-different-units-for-simultaneous-input-and-output/3069/3 "2022-03-30T09:15:31Z")

</div>

> [@Beliavsky](#):
>
> [Modern Fortran Explained: Incorporating Fortran 2018](https://oxford.universitypressscholarship.com/view/10.1093/oso/9780198811893.001.0001/oso-9780198811893) says on p278
> 
> > A file already connected to one unit must not be specified for connection to another unit.

MFE uses prohibitive clause but it makes sense, if the Standard says it is processor-dependent, both the very possibility of doing that and the results. (BTW, in my copy it is page 273).

The preceding paragraph in MFE discusses the `open` statement operation when the unit used by `open` is already connected to a file. The rules are somewhat complicated but there is one that I really do not understand.

> If the file in question does not exist, but is preconnected to the unit…

It is actually a quote from the Standard, 12.5.6.1.3. How can such a situation possibly happen?

---

<div class="post-metadata">

**Author:** ![Arjen](https://avatars.discourse-cdn.com/v4/letter/a/b9bd4f/32.png) [@Arjen](https://fortran-lang.discourse.group/u/Arjen)\
**Post date:** [March 30, 2022, 9:21am UTC](https://fortran-lang.discourse.group/t/connecting-a-file-to-different-units-for-simultaneous-input-and-output/3069/4 "2022-03-30T09:21:16Z")

</div>

Units that are preconnected include standard input, output and error. They need not be connected to a “physical” file. You do not quote much context, so I presume this is what is meant.

---

<div class="post-metadata">

**Author:** ![msz59](https://avatars.discourse-cdn.com/v4/letter/m/3d9bf3/32.png) [@msz59](https://fortran-lang.discourse.group/u/msz59)\
**Post date:** [March 30, 2022, 9:36am UTC](https://fortran-lang.discourse.group/t/connecting-a-file-to-different-units-for-simultaneous-input-and-output/3069/5 "2022-03-30T09:36:04Z")

</div>

So that would mean _reopening_ stdout etc. and connecting it to a regular file, thus making ‘redirection’ from within the program. Thanks, @Arjen.

---

<div class="post-metadata">

**Author:** ![themos](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/themos/32/521_2.png) [@themos](https://fortran-lang.discourse.group/u/themos)\
**Post date:** [March 30, 2022, 11:25am UTC](https://fortran-lang.discourse.group/t/connecting-a-file-to-different-units-for-simultaneous-input-and-output/3069/6 "2022-03-30T11:25:45Z")

</div>

Each compiler specifies which units are preconnected. For the NAG compiler, all units (apart from the 3 special ones in ISO\_FORTRAN\_ENV) are preconnected to files called fort.N (where N is the decimal representation of the unit number). This behaviour is also seen in GNU and Intel compilers.

```auto

WRITE(17,'("Hello 17")')
END

```

will result in a file fort.17 with the contents “Hello 17”.

---

<div class="post-metadata">

**Author:** ![Arjen](https://avatars.discourse-cdn.com/v4/letter/a/b9bd4f/32.png) [@Arjen](https://fortran-lang.discourse.group/u/Arjen)\
**Post date:** [March 30, 2022, 11:27am UTC](https://fortran-lang.discourse.group/t/connecting-a-file-to-different-units-for-simultaneous-input-and-output/3069/7 "2022-03-30T11:27:48Z")

</div>

So you can actually ask for those names via INQUIRE? Or is that the privilege of OPENed files? (I was unaware that it works that way 🙂 )

---

<div class="post-metadata">

**Author:** ![themos](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/themos/32/521_2.png) [@themos](https://fortran-lang.discourse.group/u/themos)\
**Post date:** [March 30, 2022, 11:49am UTC](https://fortran-lang.discourse.group/t/connecting-a-file-to-different-units-for-simultaneous-input-and-output/3069/8 "2022-03-30T11:49:39Z")

</div>

It seems to work for NAG, GNU, Intel.

---

<div class="post-metadata">

**Author:** ![urbanjost](https://avatars.discourse-cdn.com/v4/letter/u/0ea827/32.png) [@urbanjost](https://fortran-lang.discourse.group/u/urbanjost)\
**Post date:** [March 30, 2022, 1:06pm UTC](https://fortran-lang.discourse.group/t/connecting-a-file-to-different-units-for-simultaneous-input-and-output/3069/9 "2022-03-30T13:06:34Z")

</div>

On some systems you can preassign files external to Fortran. Such commands as assign allowed/allow for preconnecting units, defining their residency (if you used a particular part of a scratch file heavily you could assign part of it to memory, other parts to disk, and assign additional options to the file like compression (handy for large files);  
the standard says very little about what a file is and what preassignment is. Some compilers allow you to assign names, usually via environment variables to names that are connected to instead of something like “fort.NNNN” or “ftn.NNNN” that last I tried showed as preconnected on INQUIRE; but it has been a while.

Once a de-facto part of Fortran that never made it into the standard was preassignment via the PROGRAM directive …

PROGRAM(filea=fort10,…)

I don’t think any compilers do that anymore(?)
