# Reading data into direct access files

**URL:** <https://fortran-lang.discourse.group/t/reading-data-into-direct-access-files/2079>\
**Category:** Help\
**Created:** [October 15, 2021, 7:09am UTC](https://fortran-lang.discourse.group/t/reading-data-into-direct-access-files/2079 "2021-10-15T07:09:14Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![miramar46](https://avatars.discourse-cdn.com/v4/letter/m/e79b87/32.png) [@miramar46](https://fortran-lang.discourse.group/u/miramar46)\
**Post date:** [October 15, 2021, 7:09am UTC](https://fortran-lang.discourse.group/t/reading-data-into-direct-access-files/2079/1 "2021-10-15T07:09:14Z")

</div>

I have a bunch of data in a text file , 3rows by 53 spaces per row. I want to read this data into the computer and write it back into a direct access formatted file. The read statement I am using gives me the error message “A direct access READ statement was executed past the end of file”.  
The data I am trying to read looks like this:

```auto
1	methane CH4 16.043	190.400	46.0000
2	ammonia NH3 17.031	405.500	113.500
3	water H20 18.015	647.300	221.200

```

The read statement is:

```auto
I=2
    READ(UNIT=10,FMT=22,REC=I) RECNO(I),COMP(I),FORMULA(I),(D(I,J),J=1,3)
22 FORMAT(I3,A20,A12,3(F6.3))

```

In the OPEN statement I give a RECL=60. The sum of I3,A20,A12 AND 3(F6.3) = 53  
Not sure what the problem is. Anyone can help?  
Floyd

---

<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:** [October 15, 2021, 10:29am UTC](https://fortran-lang.discourse.group/t/reading-data-into-direct-access-files/2079/2 "2021-10-15T10:29:50Z")

</div>

Welcome to the forum. Use list-directed `read` when possible.

```
READ(UNIT=10,FMT=*) RECNO(I),COMP(I),FORMULA(I),(D(I,J),J=1,3)

```

should work

---

<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:** [October 15, 2021, 1:29pm UTC](https://fortran-lang.discourse.group/t/reading-data-into-direct-access-files/2079/3 "2021-10-15T13:29:23Z")

</div>

1. There are TAB characters in your input file, so your 22 FORMAT will get confused.

2. Your data is not in F6.3 (it’s not even F7.3 and there are spaces). Use list-directed input without a FORMAT as @Beliavsky says.

3. Direct access is usually meant for unformatted data. You cannot use list-directed (“format-less”) formatting with them. You can use the INQUIRE statement to calculate the record length.

```auto

Program toda
  Implicit None
  Integer :: recno(3), i, j, irecl
  Character :: comp(3)*20, formula(3)*12
  Real :: d(3, 3)

  Open (Unit=11, File='/tmp/data', Status='OLD')
  Do i=1,3
    Read (Unit=11, Fmt=*) recno(i), comp(i), formula(i), (d(i,j), j=1, 3)
    ! verification
    Print '(I0,A,A,3(1X,F7.3))', recno(i),comp(i),formula(i), (d(i,j),j=1,3)
  End Do
  Close (Unit=11)
  Inquire (Iolength=irecl) recno(1),comp(1),formula(1),(d(1,j), j=1, 3)
  Open (Unit=12, File='/tmp/da', Access="DIRECT", Status='NEW', &
  Recl=irecl, Form="UNFORMATTED")
  Do i=1,3
    Write(Unit=12, Rec=i) recno(i), comp(i), formula(i), &
    (d(i,j), j=1, 3)
  End Do
  Close (Unit=12)
End Program

```

---

<div class="post-metadata">

**Author:** ![miramar46](https://avatars.discourse-cdn.com/v4/letter/m/e79b87/32.png) [@miramar46](https://fortran-lang.discourse.group/u/miramar46)\
**Post date:** [October 16, 2021, 7:24am UTC](https://fortran-lang.discourse.group/t/reading-data-into-direct-access-files/2079/4 "2021-10-16T07:24:31Z")

</div>

Mr. Tsikas: I did what you suggested. First I removed all TABS from my data. Each record is exactly 60 spaces (bytes?) long

Then I changed the form to ‘unformatted’. Please ignore the commented out lines as this is just a small test program. I have a much bigger database of physical properties to read later.

However, I still have the problem with reading past the end of the file. “ An endfile record was detected in a READ statement (unit=11).

Error occurs at or near line 34 of _MAIN_\_”

I tried changing the RECL to 180 thinking it was looking at the entire file (3 rows x 60 per row) but that didn’t work either.

A record is just one row correct?

I have never been able to get the INQUIRE(IOLENGTH=RCL) output list. To work either.

Any suggestions?

Thanks,

Floyd Pfeffer

![image001.jpg](https://global.discourse-cdn.com/free1/uploads/fortran_lang/original/1X/f003192a1f3cbff81737c51a6c1ed5f5dd97394e.jpeg)

Sorry but the website would not accept the attachments. I am new to this forum and don’t know how to add some of the code so you can see what I am doing?  
(Attachment PPDBT.TXT is missing)

(Attachment READ\_DA.F90 is missing)

---

<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:** [October 16, 2021, 10:06am UTC](https://fortran-lang.discourse.group/t/reading-data-into-direct-access-files/2079/5 "2021-10-16T10:06:39Z")

</div>

You can try putting the code in [godbolt.org](http://godbolt.org) and sharing it from there.

---

<div class="post-metadata">

**Author:** ![miramar46](https://avatars.discourse-cdn.com/v4/letter/m/e79b87/32.png) [@miramar46](https://fortran-lang.discourse.group/u/miramar46)\
**Post date:** [October 16, 2021, 12:57pm UTC](https://fortran-lang.discourse.group/t/reading-data-into-direct-access-files/2079/6 "2021-10-16T12:57:06Z")

</div>

I was able to go to Godbolt and load my program into the screen, but I don’t know how to get it to you? Under share it has links to Twitter and Reddit , neither of which I use. I can join one of them but then how do I send it to you?

Floyd

![image001.jpg](https://global.discourse-cdn.com/free1/uploads/fortran_lang/original/1X/f003192a1f3cbff81737c51a6c1ed5f5dd97394e.jpeg)

---

<div class="post-metadata">

**Author:** ![mecej4](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/mecej4/32/855_2.png) [@mecej4](https://fortran-lang.discourse.group/u/mecej4)\
**Post date:** [October 16, 2021, 2:24pm UTC](https://fortran-lang.discourse.group/t/reading-data-into-direct-access-files/2079/7 "2021-10-16T14:24:52Z")

</div>

On the Godbolt page, after you have pasted in your code, you should see a Share button on the top right (and two more buttons, Policies and Other, to the right of the Share button). Click on Share, choose “Short Link”, copy the link that it gives you and paste the link in your reply here.

Alternatively, if you have one of the free email accounts such as Gmail, Outlook, etc., those accounts come with file storage. Upload your files there, obtain a link for sharing the uploaded files, and provide the link here.

---

<div class="post-metadata">

**Author:** ![miramar46](https://avatars.discourse-cdn.com/v4/letter/m/e79b87/32.png) [@miramar46](https://fortran-lang.discourse.group/u/miramar46)\
**Post date:** [October 16, 2021, 3:07pm UTC](https://fortran-lang.discourse.group/t/reading-data-into-direct-access-files/2079/8 "2021-10-16T15:07:16Z")

</div>

I think I managed to post it on Redditt. I guess you can find it?? Floyd

![image001.jpg](https://global.discourse-cdn.com/free1/uploads/fortran_lang/original/1X/f003192a1f3cbff81737c51a6c1ed5f5dd97394e.jpeg)

---

<div class="post-metadata">

**Author:** ![miramar46](https://avatars.discourse-cdn.com/v4/letter/m/e79b87/32.png) [@miramar46](https://fortran-lang.discourse.group/u/miramar46)\
**Post date:** [October 16, 2021, 3:34pm UTC](https://fortran-lang.discourse.group/t/reading-data-into-direct-access-files/2079/9 "2021-10-16T15:34:19Z")

</div>

OK, thanks , here is the link to my code. I am having trouble with the read statement on line 35. I get an end of file error. Don’t understand why.

When you ask Fortran to read an unformatted file, does it read the first row space by space picking up numbers,+/- and decimals until it reaches a blank. Then the next number,+,- begins the next number? I lack the basic understanding of what is going on in the Read.

Thanks for your help,

Floyd Pfeffer

[https://godbolt.org/z/E5Gbof7hx](https://godbolt.org/z/E5Gbof7hx)

![image001.jpg](https://global.discourse-cdn.com/free1/uploads/fortran_lang/original/1X/f003192a1f3cbff81737c51a6c1ed5f5dd97394e.jpeg)

---

<div class="post-metadata">

**Author:** ![mecej4](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/mecej4/32/855_2.png) [@mecej4](https://fortran-lang.discourse.group/u/mecej4)\
**Post date:** [October 16, 2021, 4:07pm UTC](https://fortran-lang.discourse.group/t/reading-data-into-direct-access-files/2079/10 "2021-10-16T16:07:44Z")

</div>

Please upload a good copy of your original input file (with the chemical data that you wish to read).

The program that you posted at Godbolt is bad and harmful. It opens the input file and reads it as a text file using list-directed READs, but then _overwrites_ the file as an unformatted direct access file. I hope that you have a backup of the file.

---

<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:** [October 16, 2021, 4:18pm UTC](https://fortran-lang.discourse.group/t/reading-data-into-direct-access-files/2079/11 "2021-10-16T16:18:43Z")

</div>

Your original file is clearly formatted, it is human readable. You should be OPENing that file with ACCESS=“SEQUENTIAL”, FORM=“FORMATTED”, STATUS=“OLD”. You should be OPENing a different unit (and different filename) with ACCESS=“DIRECT”, FORM=“UNFORMATTED”, STATUS=“NEW”. You then write a loop reading and writing each record. Your new file can then be accessed directly, at random and fast, but won’t be human readable.

I still don’t know why your READ goes wrong. We need to see the exact binary form of that file. And we don’t know what operating system you are using or which compiler. We can help more if you tell us.

---

<div class="post-metadata">

**Author:** ![miramar46](https://avatars.discourse-cdn.com/v4/letter/m/e79b87/32.png) [@miramar46](https://fortran-lang.discourse.group/u/miramar46)\
**Post date:** [October 16, 2021, 5:04pm UTC](https://fortran-lang.discourse.group/t/reading-data-into-direct-access-files/2079/12 "2021-10-16T17:04:03Z")

</div>

I just posted a copy of the data file. I don’t understand what is harmful about what I am doing? Please elaborate. Thanks, Floyd

![image001.jpg](https://global.discourse-cdn.com/free1/uploads/fortran_lang/original/1X/f003192a1f3cbff81737c51a6c1ed5f5dd97394e.jpeg)

---

<div class="post-metadata">

**Author:** ![miramar46](https://avatars.discourse-cdn.com/v4/letter/m/e79b87/32.png) [@miramar46](https://fortran-lang.discourse.group/u/miramar46)\
**Post date:** [October 16, 2021, 5:12pm UTC](https://fortran-lang.discourse.group/t/reading-data-into-direct-access-files/2079/13 "2021-10-16T17:12:01Z")

</div>

[https://godbolt.org/z/aGMdfsT7n](https://godbolt.org/z/aGMdfsT7n)

Try this link for the database file. Floyd

![image001.jpg](https://global.discourse-cdn.com/free1/uploads/fortran_lang/original/1X/f003192a1f3cbff81737c51a6c1ed5f5dd97394e.jpeg)

---

<div class="post-metadata">

**Author:** ![mecej4](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/mecej4/32/855_2.png) [@mecej4](https://fortran-lang.discourse.group/u/mecej4)\
**Post date:** [October 16, 2021, 5:14pm UTC](https://fortran-lang.discourse.group/t/reading-data-into-direct-access-files/2079/14 "2021-10-16T17:14:30Z")

</div>

Where did you post the data file?

Your program at Godbolt opens a file named PPDBT at a formatted sequential file on unit 11, reads three lines from it and closes the file.

It then opens the same file on unit 12 as a direct access unformatted file, writes three records to it, and closes the file.

The original file can be clobbered as a result of these two actions, unless some other erroneous I/O operation occurred earlier and caused the program to abort prematurely.

---

<div class="post-metadata">

**Author:** ![miramar46](https://avatars.discourse-cdn.com/v4/letter/m/e79b87/32.png) [@miramar46](https://fortran-lang.discourse.group/u/miramar46)\
**Post date:** [October 16, 2021, 5:33pm UTC](https://fortran-lang.discourse.group/t/reading-data-into-direct-access-files/2079/15 "2021-10-16T17:33:46Z")

</div>

I am using a Windows 10 operating system. My compiler is Lahey-Fujitsu Express Fortran v7.3 windows 32 bit.

I sent you the data file but it is in txt format. I don’t know how to convert it to binary?

Floyd

![image001.jpg](https://global.discourse-cdn.com/free1/uploads/fortran_lang/original/1X/f003192a1f3cbff81737c51a6c1ed5f5dd97394e.jpeg)

---

<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:** [October 16, 2021, 5:38pm UTC](https://fortran-lang.discourse.group/t/reading-data-into-direct-access-files/2079/16 "2021-10-16T17:38:05Z")

</div>

As @mecej4 said, you should not be opening the same file (same name) twice with two different units and different access modes. I think that is why the compiler complains

---

<div class="post-metadata">

**Author:** ![miramar46](https://avatars.discourse-cdn.com/v4/letter/m/e79b87/32.png) [@miramar46](https://fortran-lang.discourse.group/u/miramar46)\
**Post date:** [October 16, 2021, 6:07pm UTC](https://fortran-lang.discourse.group/t/reading-data-into-direct-access-files/2079/17 "2021-10-16T18:07:06Z")

</div>

I used Godbolt to make a link and I pasted it into a reply to either your message or themos’s message. Can you not find it? Floyd

![image001.jpg](https://global.discourse-cdn.com/free1/uploads/fortran_lang/original/1X/f003192a1f3cbff81737c51a6c1ed5f5dd97394e.jpeg)

---

<div class="post-metadata">

**Author:** ![miramar46](https://avatars.discourse-cdn.com/v4/letter/m/e79b87/32.png) [@miramar46](https://fortran-lang.discourse.group/u/miramar46)\
**Post date:** [October 16, 2021, 6:12pm UTC](https://fortran-lang.discourse.group/t/reading-data-into-direct-access-files/2079/18 "2021-10-16T18:12:04Z")

</div>

I am new to this site and don’t quite understand how to get information to you. I have gone to Godbolt and attached my physical property file, PPDBT.txt and then shared the link (short link) back to your email. Unfortunately their system thinks my post is spam and deleted it. Can you explain to me, how to properly send you the file?

Sorry for the hassle.

Floyd Pfeffer

![image001.jpg](https://global.discourse-cdn.com/free1/uploads/fortran_lang/original/1X/f003192a1f3cbff81737c51a6c1ed5f5dd97394e.jpeg)

---

<div class="post-metadata">

**Author:** ![mecej4](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/mecej4/32/855_2.png) [@mecej4](https://fortran-lang.discourse.group/u/mecej4)\
**Post date:** [October 16, 2021, 6:15pm UTC](https://fortran-lang.discourse.group/t/reading-data-into-direct-access-files/2079/19 "2021-10-16T18:15:46Z")

</div>

I can find the [3-line data file](https://godbolt.org/z/aGMdfsT7n) now.

The program source code that @themos posted above works fine on the data file and produces the corresponding unformatted direct access file, once you insert the file names that you wish to use on your system for the input and output files.

---

<div class="post-metadata">

**Author:** ![miramar46](https://avatars.discourse-cdn.com/v4/letter/m/e79b87/32.png) [@miramar46](https://fortran-lang.discourse.group/u/miramar46)\
**Post date:** [October 17, 2021, 6:10pm UTC](https://fortran-lang.discourse.group/t/reading-data-into-direct-access-files/2079/20 "2021-10-17T18:10:04Z")

</div>

I don’t understand why it works for you but I continue to get the An endfile record was detected in a READ statement (unit=10).

Error occurs at or near line 20 of _MAIN_\_

Here is the current code I am using. I tried to link it to you thru Godbolt but it doesn’t seem to work?? Floyd

```auto
PROGRAM READ_DA

! This is a test program to read data from 'PPDBT' and print it back to the screen

IMPLICIT NONE

REAL ::D(50,50)

INTEGER ::I,J,IOS,RECNO(50),IRCL

CHARACTER*8 ::FILENAME

CHARACTER*20 ::COMP(50)

CHARACTER*20 ::FORMULA(50)

IOS=0

!

! Now open 'PPDBT'AS SEQUENTIAL AND READ ALL RECORDS

!

OPEN (UNIT=10,FILE='PPDBT',ACCESS='SEQUENTIAL',FORM='FORMATTED',STATUS='OLD',IOSTAT=IOS)

IF(IOS/=0) THEN

PRINT *, "ERROR OPENING FILE PPDATA"

END IF

DO I=1,3

READ(UNIT=10,FMT=*) RECNO(I),COMP(I),FORMULA(I),(D(I,J),J=1,3)

! 22 FORMAT(I3,A20,A12,3(F7.3))

! 20 FORMAT(I4,A20,A12,4(F8.3),8(E12.5),2(F6.1),6(F10.5),4(E10.4),2(F6.1),5(F8.3))

PRINT *, RECNO(I),COMP(I),FORMULA(I),(D(I,J),J=1,3)

END DO

CLOSE(UNIT=10)

INQUIRE(IOLENGTH=IRCL) RECNO(1),COMP(1),FORMULA(1),(D(1,J),J=1,3)

OPEN(UNIT=12,FILE='DBPPT',ACCESS='DIRECT',STATUS='NEW',RECL=IRCL,FORM='UNFORMATTED')

DO I=1,3

WRITE(UNIT=12,REC=I) RECNO(I),COMP(I),FORMULA(I),(D(I,J),J=1,3)

END DO

CLOSE (UNIT=12)

```

![image001.jpg](https://global.discourse-cdn.com/free1/uploads/fortran_lang/original/1X/f003192a1f3cbff81737c51a6c1ed5f5dd97394e.jpeg)

[Next page](https://fortran-lang.discourse.group/t/reading-data-into-direct-access-files/2079.md?page=2)
