Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Help with I/O using Direct access & sequential

Status
Not open for further replies.

nikhill

Technical User
Mar 4, 2010
3
US
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
 
Direct access files store the values in binary: exact numeric representation as used by the machine.

The text output is rounded to the nearest decimal equivalent. It will only sometimes have same precision as the binary representation. Take 3.0. If you print it to enough places, you might get 2.9999.. or 3.00...and some rubbish. Never 3.0..0. If you take 4.0, chances are you will get 4.0...0 because it is a multiple of 2. Binary is a base 2 system.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top