Re: Write position
- From: Arjen Markus <arjen.markus@xxxxxxxxxx>
- Date: Tue, 3 Feb 2009 07:09:10 -0800 (PST)
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
Regards,
Arjen
.
- Follow-Ups:
- Re: Write position
- From: Dave Allured
- Re: Write position
- References:
- Write position
- From: Bokgae
- Re: Write position
- From: glen herrmannsfeldt
- Re: Write position
- From: Arjen Markus
- Re: Write position
- From: Bokgae
- Re: Write position
- From: Clive Page
- Write position
- Prev by Date: Re: Write position
- Next by Date: Re: Intrinsic functions and non-trivial lower bounds
- Previous by thread: Re: Write position
- Next by thread: Re: Write position
- Index(es):
Relevant Pages
|
Loading