简体   繁体   中英

Opening/reading .dat file in Fortran77

I'm trying to read through a set of points in a .dat file and to run the points through an algorithm in my .f file. I've been working with the OPEN statement:

OPEN(UNIT=1,FILE='POINTS.DAT',FORM='UNFORMATTED')

and for a starter, have been trying to print out the values in the .dat - not working.

PRINT *, 1

PRINT *, POINTS.DAT

PRINT *, 'POINTS.DAT'

Should I ditch trying to print out the values? How should I be indexing the values in the .dat? Should I do a DO loop and have it loop n times through my the number of data points in my file? How do I call those values?

After the open you have to read the values from the file. You can't print them by using the file name in a print statement. If you are correct that is is a binary / unformatted file, you use a read without a format: read (1) item . You can either use a do loop and read one item at a time or read all the items into an array. If the file was written with another Fortran program, you should use the same method (single item or array) as was used for writing because the file will be record based. If the file was written with another language, add access='stream' to your open statement to inform Fortran that it doesn't have the record structure normally used by Fortran.

Basic structure 1:

real :: item  ! or whatever type
open (unit=1, ...
ReadLoop: do
  read (1, end=99) item
  process item...
end do ReadLoop
99 continue

Basic structure 2:

real, dimension (NUMBER) :: array
open (unit=1,..
read (1) array
process array...

This is basic Fortran ... you will probably make faster progress if you find a book to learn from. I like Fortran 90/95 Explained by Metcalf and Reid.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM