Reading data into direct access files

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:

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:

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

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

  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.


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
1 Like

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

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)

You can try putting the code in godbolt.org and sharing it from there.

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

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.

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

image001.jpg

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

image001.jpg

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.

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.

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://godbolt.org/z/aGMdfsT7n

Try this link for the database file. Floyd

image001.jpg

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.

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

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

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

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

I can find the 3-line data file 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.

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

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