Re: Write after nonadvancing read



On 2008-02-09 12:34:29 -0400, nospam@xxxxxxxxxxxxx (Richard Maine) said:

Thomas Koenig <tkoenig@xxxxxxxxxxxxx> wrote:

Should the following program do what I expect it to do, i.e.
print " ab12" ?

program main
character(len=2) :: c
character(len=4) :: d
write (10,'(A)') 'abcd'
rewind 10
read (10,'(A2)',advance="no") c
write(10,'(A)') '12'
rewind 10
read (10,'(A)') d
print *,d
end program main
...
The same should be true for stream I/O.

Is my analysis correct, or did I miss something?

I believe you have it right.

Well, there is a silly caveat that has nothing to do with the
nonadvancing stuff and also won't be a problem in practice, but just to
pick a nit as I'm looking for anything that could go wrong...

You don't have an OPEN statement. In particular, you don't have an OPEN
statement with pos='rewind'. The standard allows this to open an
existing file at a position other than the beginning, which would
certainly not give the result that you expect. There used to be systems
on which this could actually happen; I don't think there are any today,
but the standard allows it.

I recall using this trick of positioning a file in one step and
starting from that in the next on OS/360. That was some time ago as
360 has become 370 etc. They have very good historical continuity
so I would be surprised if it did not still work. For some reason
I think that it was safe to assume that the file was at the beginning
for the whole job, but clearly not for each step.

The notion comes from the physical properties of tape drives.


.



Relevant Pages

  • Re: Write after nonadvancing read
    ... write ') 'abcd' ... You don't have an OPEN statement. ... but the standard allows it. ...
    (comp.lang.fortran)
  • Re: Why does rewind() ignore errors?
    ... both simpler and more useful for rewind() to return the same int ... Just imagine the C standard at the time where the C standard was not ... If the C standard had said that it returned the same value than fseek, ...
    (comp.std.c)
  • Re: Error writing on unit 22
    ... read/rewind/write, for portability. ... Rewind is extremely common and portable. ... I'd have to go drag out the standard to be sure ...
    (comp.lang.fortran)