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

  • Fortran
  • Thread starter nikhillaghave
  • Start date
  • Tags
    Fortran
In summary, the conversation discusses the implementation of simple check-pointing in an application using direct access unformatted files and free-format specifiers. However, the file needed to be human readable/editable, so it was changed to a sequential file with free-format write and read. However, this resulted in a loss of precision and incorrect results. The conversation ends with a request for help in resolving the issue and a clarification that the issue is not related to direct or sequential access, but rather the use of formatted versus binary files.
  • #1
nikhillaghave
2
0
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
 
Technology news on Phys.org
  • #2
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.
 

1. How do I open a file in Direct Access mode in Fortran?

To open a file in Direct Access mode in Fortran, you need to use the "OPEN" statement with the "ACCESS" specifier set to "DIRECT". Example:
OPEN(UNIT=1,FILE='filename',ACCESS='DIRECT')

2. How do I read from a file in Direct Access mode in Fortran?

To read from a file in Direct Access mode in Fortran, you need to use the "READ" statement with the "RECL" specifier set to the desired record length. Example:
READ(1,RECL=100) data

3. How do I write to a file in Direct Access mode in Fortran?

To write to a file in Direct Access mode in Fortran, you need to use the "WRITE" statement with the "RECL" specifier set to the desired record length. Example:
WRITE(1,RECL=100) data

4. How do I open a file in Sequential Access mode in Fortran?

To open a file in Sequential Access mode in Fortran, you need to use the "OPEN" statement with the "ACCESS" specifier set to "SEQUENTIAL". Example:
OPEN(UNIT=1,FILE='filename',ACCESS='SEQUENTIAL')

5. How do I read from a file in Sequential Access mode in Fortran?

To read from a file in Sequential Access mode in Fortran, you need to use the "READ" statement without any additional specifiers. Example:
READ(1) data

Similar threads

  • Programming and Computer Science
Replies
12
Views
2K
  • Programming and Computer Science
Replies
5
Views
4K
  • Programming and Computer Science
Replies
4
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
7
Views
1K
  • Programming and Computer Science
Replies
8
Views
1K
  • Programming and Computer Science
Replies
12
Views
1K
  • Programming and Computer Science
Replies
5
Views
1K
  • Programming and Computer Science
Replies
6
Views
1K
  • Programming and Computer Science
Replies
6
Views
967
  • Programming and Computer Science
Replies
16
Views
3K
Back
Top