Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

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

  1. Mar 19, 2010 #1

    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 (Text):

    C Write
         +        access='DIRECT',RECL=recordlength)

    C READ
         +        ACCESS='DIRECT',RECL=recordlength)

         +        (X(i),i=1,N),F,NF

    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 (Text):

    C write
         +        access='APPEND')
             write(unit,*) (X(I),I=1,N),F,NF

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

    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.

  2. jcsd
Share this great discussion with others via Reddit, Google+, Twitter, or Facebook

Can you offer guidance or do you also need help?
Draft saved Draft deleted