Re: Write position



Arjen Markus wrote:

On 3 feb, 15:51, Clive Page <j...@xxxxxxxxxxxx> wrote:
It seems a rather simple request, doesn't it: to write a file which ends
up with something in the first line telling you how many lines follow.
But it's surprisingly hard to do in Fortran. Here are a few options
that occur to me:

(1) Plain text (formatted sequential) files: if you rewind the file and
write to the first record, you instantly truncate the file to that
length, so all the earlier stuff gets lost. So that's no solution.

(2) Direct-access file (default unformatted variety): you can re-write
record 1 without zapping the rest, but have to work around several
awkward requirements:
- you need to choose the record length, in units which are operating
system-dependent (solve this using INQUIRE by IOLENGTH).
- each write needs to append a new-line sequence, which is operating
system dependent (Fortran2003 provides a NEW_LINE function but as far as
I can see it isn't useful for this nor anything else). For Windows you
need both CR and LF, so need to leave 2 bytes free at the end of the
record
- you need to use an internal-file write to get a character string of
the right length (including the newline) which you can then write to the
record.

(3) Formatted direct-access file: nearly as complicated as (2) except
that the record-length is not generally o/s-dependent, and you can use a
formatted write (but not list-directed, of course), but you need to get
the format correct to fill the record (including the newline sequence).

(4) Write data to a scratch file (as Glen H suggested earlier) then
re-read it all after having written the record count to the desired
output file. Complicated and inefficient, but would work.

(5) Write to a formatted stream file (supported by g95, gfortran, and
several other compilers, but strictly a Fortran2003 feature):
unfortunately again when you re-write record 1, you truncate the file at
that point, so this does not work.

(6) Write to an unformatted stream file (ditto). Here you can re-write
record 1 (but you need to reserve space for it initially, of course)
without truncating the file at that point. As for direct-access you
need to convert the numbers to text yourself with an internal file
write, and append the newline sequences yourself, but a minor advantage
is that you don't have to worry about record lengths at all. Works -
but complicated to do.

(7) Write the record count to a second text file. After the program
terminates use the commands of the operating system to append the first
file to the second, and delete the first file. Nearly as inefficient as
(4), but the Fortran code is probably the simplest of all.

Hmm, none of these looks anything like an ideal solution to me. Maybe
someone else has an idea that I missed...

--
Clive Page

This type of things is not only troublesome in Fortran, but in any
language I know of: sequential file I/O is simply not meant for this.

On the other hand, OP specifically uses an array RESULT - perhaps
we are trying too hard:

NCOUNT = 0
OPEN( UNIT = 10, STATUS = 'SCRATCH' )
DO N = 1, 1000000000, 1
NCOUNT = NCOUNT + 1
RESULT( N ) = some calculations
!WRITE( 10, * ) RESULT( N )
IF( RESULT( N ) > 10. ) EXIT
ENDDO
OPEN( UNIT = 99, FILE = 'fort.99', STATUS = 'REPLACE' )
WRITE( 99, * ) NCOUNT
DO N = 1,NCOUNT
WRITE( 99, * ) RESULT(N)
ENDDO

Good one Arjen. I have several programs that do essentially this, save
results in local arrays, then write headers, counts, and the final data
sequentially during the second phase of the program.

I think this is a better method when the result arrays are both simple
enough and short enough. When the result arrays become cumbersome, it's
time to figure out how to update header information after writing the
main file.

--Dave
.



Relevant Pages

  • Re: Write position
    ... But it's surprisingly hard to do in Fortran. ... the format correct to fill the record (including the newline sequence). ... NCOUNT = NCOUNT + 1 ...
    (comp.lang.fortran)
  • Re: Write position
    ... the format correct to fill the record (including the newline sequence). ... integer ncount, n ...
    (comp.lang.fortran)
  • Re: Fuzzy matching of postal addresses [1/1]
    ... >position along the sequence. ... As to Python not being Fortran. ... Andrew McLean ...
    (comp.lang.python)
  • Re: Fortran is eating my brain
    ... Last few years of my life i have learned many languages (otherthan ... Nowadays i use perl python tcl etc for creating traslating and parsers ... first one writes the output file using fortran units wither in UNFMTD ... Once upon a time formatted data was only available if you used a FORMAT but then a variety of ways for having the system use some format, of its convenience, arrived. ...
    (comp.lang.fortran)
  • Re: speed up calculation suggestions
    ... Fortran matter. ... same internal formats - the ones that the CPU supports. ... the large majority of machines today use the same format. ...
    (comp.lang.fortran)