Fortran: help with I/O in Direct Access and Seq. access

  • #1

Main Question or Discussion Point

Hello,

I am implementing simple check-pointing in an application so that the history is saved to a file and upon restart the data is read from this file.
-> The history file contains an array of real*8 numbers and precision is very important in the program.

I first implemented this using direct access unformatted file and it worked fine. Basically the default free-format specifiers used by fortran had the same precision while writing and reading the data.
Code:
C Write
         open(unit,file='hist.dat',
     +        access='DIRECT',RECL=recordlength)
         write(unit,REC=NF),(X(I),I=1,N),F,NF
         close(unit)

C READ
         open(unit,file='hist.dat',status='old',
     +        ACCESS='DIRECT',RECL=recordlength)

         read(unit,REC=NF),
     +        (X(i),i=1,N),F,NF

         close(unit)
I wanted the history file to be human readable/editable, so I made the file sequential and used the free-format write(unit,*) and read(unit,*) for writing and reading the data. Since the defaults worked in direct access file, i assumed it should work in sequential too, but the precision became incorrect and as a result the history file does not work the way it should and gives incorrect results on restarts. The data looks the same to the naked eye, but some where the precision is lost.

Code:
C write
     open(unit,file='hist.dat',
     +        access='APPEND')
         write(unit,*) (X(I),I=1,N),F,NF
         close(unit)

C read
     open(unit,file='hist.dat')
            read(unit,*)
     +           (X(i),i=1,N),F,NF

           close(unit)
Can some one explain why this is happening? is it because direct access files store data according to the record length?

How can make the seq. I/O format specifiers exactly the same as the direct I/O ?

Can someone point me in the right direction as I am clueless at this point. Thank You.

Nikhil
 

Answers and Replies

  • #2
8,495
5,373
I don't think the problem is direct or sequential, it is formatted versus binary.

A binary file retains full precision. It is not human readable.

Formatted output is human readable. It may round the results.
 

Related Threads on Fortran: help with I/O in Direct Access and Seq. access

Replies
7
Views
2K
  • Last Post
Replies
3
Views
862
Replies
1
Views
1K
  • Last Post
Replies
1
Views
2K
  • Last Post
Replies
2
Views
2K
  • Last Post
Replies
3
Views
2K
Replies
1
Views
2K
Replies
3
Views
47K
  • Last Post
Replies
4
Views
3K
Top