As I just said, with FXDR you open a file for either reading or writing using a different call, then access the file for either with the same code. Fortran is exactly the opposite: you open the file with the same code for either reading or writing, then you use different calls to accomplish either reading or writing. Of the two methods, FXDR is superior. Why? Because there is less chance that you will screw up writing out and then subsequently reading in the same file, since it is exactly the same code doing both the reading and writing (with FXDR). With Fortran, it is easy to add another parameter to the list of what you write out but then forget to modify the reading-in code to read in that new parameter.
Here is a simple example. To write a file containing the state of your numerical code, which depends on param1 through param3 and 'bigdataarray':
Step 1. Write out (create) the state file.
subroutine writestate open(10,file='savestate.dat',form='unformatted') write(10) param1 write(10) param2 write(10) param3 write(10) bigdataarray close(10)
Step 2. Read in the state file back in.
subroutine readstate open(10,file='savestate.dat',form='unformatted') read(10) param1 read(10) param2 read(10) param3 read(10) bigdataarray close(10)
You have to write all the code twice, once with 'write's and once with 'read's. If you later decide to save a new parameter, say, param4, and add it to the writestate routine, you must remember to add it to the readstate routine as well. That's pretty simple for the brief example shown here, but with a complicated program which can have dozens or hundreds of saved parameters it can be quite a chore. In any event, it's needlessly error-prone.
Step 1. Put all your file access calls in one subroutine:
subroutine dostate( mode ) ! mode is either 'r' or 'w' character*1 mode ixdr = initxdr( 'savestate.xdr', mode, .FALSE. ) ierr = ixdrreal( ixdr, param1 ) ierr = ixdrreal( ixdr, param2 ) ierr = ixdrreal( ixdr, param3 ) ierr = ixdrrmat( ixdr, nelements, bigdataarray )
Step 2. Call the file access subroutine with whichever mode you want:
To write (create) the state file:
call dostate( 'w' )
To read the state file:
call dostate( 'r' )
There is no way to add a new parameter which is written to the state file without also addiing it in to be read from that same state file.
Last modified Sep 2, 1999