# Direct file access in FORTRAN 77

**URL:** <https://fortran-lang.discourse.group/t/direct-file-access-in-fortran-77/1133>\
**Category:** Help\
**Created:** [April 30, 2021, 4:40pm UTC](https://fortran-lang.discourse.group/t/direct-file-access-in-fortran-77/1133 "2021-04-30T16:40:24Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![interkosmos](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/interkosmos/32/296_2.png) [@interkosmos](https://fortran-lang.discourse.group/u/interkosmos)\
**Post date:** [April 30, 2021, 4:40pm UTC](https://fortran-lang.discourse.group/t/direct-file-access-in-fortran-77/1133/1 "2021-04-30T16:40:24Z")

</div>

I’m trying to read a binary file in FORTRAN 77 with `ACCESS='DIRECT'`, but unfortunately, without success. The example function opens a file in direct mode/unformatted, and tries to read all data byte by byte (in this case, each byte just into character `A`):

```auto
      CHARACTER A
      CHARACTER*32 MSG
      INTEGER ISTAT, NBYTES

      NBYTES = 1024

      OPEN (UNIT=10, ACCESS='DIRECT', ACTION='READ', FILE=PATH,
     & FORM='UNFORMATTED', IOSTAT=ISTAT, RECL=NBYTES,
     & STATUS='OLD')
      IF (ISTAT .NE. 0) RETURN

      DO 10 I = 1, NBYTES
      READ (10, IOSTAT=ISTAT, IOMSG=MSG, REC=I) A

      IF (ISTAT .NE. 0) THEN
        PRINT *, ISTAT, MSG
        CLOSE (10)
        RETURN
      END IF

   10 CONTINUE

      CLOSE (10) 

```

The file is 1024 bytes in size. The first byte is read successfully, but the second one always fails with error “non-existing record number” (5002).

Using `ACCESS='STREAM'` instead, which is not a FORTRAN 77 feature, it works:

```auto
      OPEN (UNIT=10, ACCESS='STREAM', ACTION='READ', FILE=PATH,
     & FORM='UNFORMATTED', IOSTAT=ISTAT, STATUS='OLD')
      IF (ISTAT .NE. 0) RETURN

      DO 10 I = 1, NBYTES
      READ (10, IOSTAT=ISTAT, IOMSG=MSG) A
      IF (ISTAT .NE. 0) THEN
        PRINT *, ISTAT, MSG, I
        CLOSE (10)
        RETURN
      END IF

   10 CONTINUE

```

Does someone know what I’m doing wrong?

---

<div class="post-metadata">

**Author:** ![interkosmos](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/interkosmos/32/296_2.png) [@interkosmos](https://fortran-lang.discourse.group/u/interkosmos)\
**Post date:** [April 30, 2021, 5:02pm UTC](https://fortran-lang.discourse.group/t/direct-file-access-in-fortran-77/1133/3 "2021-04-30T17:02:52Z")

</div>

The `RECL` argument in the `OPEN` statement expects different values for reading and writing. In order to write a file, the whole data size must be given (in this case, 1024 bytes), while to read, the size of a single record (1 byte) has to be set.

---

<div class="post-metadata">

**Author:** ![billlong](https://avatars.discourse-cdn.com/v4/letter/b/71e660/32.png) [@billlong](https://fortran-lang.discourse.group/u/billlong)\
**Post date:** [April 30, 2021, 8:37pm UTC](https://fortran-lang.discourse.group/t/direct-file-access-in-fortran-77/1133/4 "2021-04-30T20:37:43Z")

</div>

The right solution is to use ACCESS=“STREAM”. FORTRAN 77 as a standard was deleted long ago, and I don’t know of any current compilers that are limited to what was in that spec. There is nothing wrong with using modern Fortran features.

---

<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:** [April 30, 2021, 9:17pm UTC](https://fortran-lang.discourse.group/t/direct-file-access-in-fortran-77/1133/5 "2021-04-30T21:17:45Z")

</div>

In fact, the sample code already uses a non-FORTRAN 77 feature: ISOMSG = MSG

Also note that the RECL attribute may have to specified as the number of “words” in stead of the number of bytes - depending on the compiler.

Your best bet is indeed ACCESS = “STREAM”

---

<div class="post-metadata">

**Author:** ![certik](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/certik/32/4_2.png) [@certik](https://fortran-lang.discourse.group/u/certik)\
**Post date:** [April 30, 2021, 10:26pm UTC](https://fortran-lang.discourse.group/t/direct-file-access-in-fortran-77/1133/6 "2021-04-30T22:26:54Z")

</div>

> [@billlong](#):
>
> FORTRAN 77 as a standard was deleted long ago

What does it mean to “delete a standard”?

---

<div class="post-metadata">

**Author:** ![interkosmos](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/interkosmos/32/296_2.png) [@interkosmos](https://fortran-lang.discourse.group/u/interkosmos)\
**Post date:** [April 30, 2021, 11:32pm UTC](https://fortran-lang.discourse.group/t/direct-file-access-in-fortran-77/1133/7 "2021-04-30T23:32:12Z")

</div>

> [@Arjen](#):
>
> In fact, the sample code already uses a non-FORTRAN 77 feature: ISOMSG = MSG

I know, just added it for testing. I try to stick to FORTRAN 77 to be able to use f2c as a compiler, but I already regret it. For instance, array slicing was not introduced until Fortran 90. Therefore, I have to stick to [the FORTRAN 66 way](https://en.wikipedia.org/wiki/Array_slicing#1966:_Fortran_66). Crazy.

---

<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:** [May 3, 2021, 9:45pm UTC](https://fortran-lang.discourse.group/t/direct-file-access-in-fortran-77/1133/8 "2021-05-03T21:45:44Z")

</div>

> [@interkosmos](#):
>
> The `RECL` argument in the `OPEN` statement expects different values for reading and writing. In order to write a file, the whole data size must be given (in this case, 1024 bytes), while to read, the size of a single record (1 byte) has to be set.

So, as you point above, use RECL=1 for reading.  
BTW, the `ACTION='READ'` spec is also non-F77, AFAIR.

---

<div class="post-metadata">

**Author:** ![billlong](https://avatars.discourse-cdn.com/v4/letter/b/71e660/32.png) [@billlong](https://fortran-lang.discourse.group/u/billlong)\
**Post date:** [May 29, 2021, 2:09am UTC](https://fortran-lang.discourse.group/t/direct-file-access-in-fortran-77/1133/9 "2021-05-29T02:09:26Z")

</div>

The technical ISO term is “cancels”. Each new standard has a sentence like this one in F2018:

" 1. This fourth edition cancels and replaces the third edition (ISO 1539-1:2010), which has been technically revised."

All old versions are serially cancelled by their replacements.

(ISO 1539-1:2010 was the formal name of the Fortran 2008 standard.)

---

<div class="post-metadata">

**Author:** ![certik](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/certik/32/4_2.png) [@certik](https://fortran-lang.discourse.group/u/certik)\
**Post date:** [May 30, 2021, 2:05am UTC](https://fortran-lang.discourse.group/t/direct-file-access-in-fortran-77/1133/10 "2021-05-30T02:05:21Z")

</div>

I see, thanks!
