Re: Write position



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
.



Relevant Pages

  • Re: Write position
    ... the format correct to fill the record (including the newline sequence). ... integer ncount, n ...
    (comp.lang.fortran)
  • 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
    ... 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: Restore Form Field in Customize Outline Number List
    ... make the "button clicking" sequence very clear... ... read up on Styles and Outline Numbering. ... "Stefan Blom" wrote: ... reinsert a form field in the Number format box? ...
    (microsoft.public.word.docmanagement)
  • Re: Questions on reuse of FORMAT statements and newer I/O options ?such as ACCESS=STREAM
    ... Fortran standard that relates stream vs sequential to those ways, ... "A file is composed of either a sequence of file storage units ... I/O, slash edit descriptors, or format reversion. ...
    (comp.lang.fortran)